天天看点

如何获取手机短信和通话记录信息

android 下不同进程是通过ContentResolver共享数据的,下面通过这个类去查询手机上所有联系人的信息和通话记录,包含号码、内容、日期,通话时长等信息

一、获取手机短信信息

public void getSmsInPhone()   
	{   
	    final String SMS_URI_ALL   = "content://sms/";         
	       
	    try{   
	        ContentResolver cr = getContentResolver();   
	        String[] projection = new String[]{"_id", "address", "person",    
	                "body", "date", "type"};   
	        Uri uri = Uri.parse(SMS_URI_ALL);   
	        Cursor cur = cr.query(uri, projection, null, null, "date desc");   
	  
	        if (cur.moveToFirst()) {     
	            String phoneNumber;          
	            String smsbody;   
	            String date;   
	            String type;   
	            	  
	            int phoneNumberColumn = cur.getColumnIndex("address");   
	            int smsbodyColumn = cur.getColumnIndex("body");   
	            int dateColumn = cur.getColumnIndex("date");   
	            int typeColumn = cur.getColumnIndex("type");   
	            
	            do{   
	                phoneNumber = cur.getString(phoneNumberColumn); 
	                smsbody = cur.getString(smsbodyColumn);   
	                   
	                SimpleDateFormat dateFormat = new SimpleDateFormat(   
	                        "yyyy-MM-dd hh:mm:ss");   
	                Date d = new Date(Long.parseLong(cur.getString(dateColumn)));   
	                date = dateFormat.format(d);   
	                   
	                int typeId = cur.getInt(typeColumn);   
	                if(typeId == 1){   
	                    type = "接收";   
	                } else if(typeId == 2){   
	                    type = "发送";   
	                } else {   
	                    type = "";   
	                }   
	                
	                Message msg = mHandler.obtainMessage(0);  
	                Bundle b = new Bundle();// 存放数据  
		            b.putString("phone", phoneNumber);
	                b.putString("smsbody", smsbody);
	                b.putString("data", date);
	                b.putString("type", type);
	                msg.setData(b);  
	                msg.sendToTarget();  
	                             
	                
	                if(smsbody == null) smsbody = "";     
	            }while(cur.moveToNext());   
	        } 
	    } catch(SQLiteException ex) {   
	        Log.d("SQLiteException in getSmsInPhone", ex.getMessage());   
	    }    
	}  
           

二、获取通话记录

private void GetCallsInPhone()
	{
		Cursor cursor = getContentResolver().query(Calls.CONTENT_URI,  
			    new String[] { Calls.DURATION, Calls.TYPE, Calls.DATE, Calls.NUMBER },  
			    null,  
			    null,  
			    Calls.DEFAULT_SORT_ORDER);  
			MainActivity.this.startManagingCursor(cursor);  
			boolean hasRecord = cursor.moveToFirst();  
			long incoming = 0L;  
			long outgoing = 0L;  
			int count = 0;  
			String strPhone = "";
			String date; 
			
			while (hasRecord) {  
			    int type = cursor.getInt(cursor.getColumnIndex(Calls.TYPE));  
			    long duration = cursor.getLong(cursor.getColumnIndex(Calls.DURATION));  
			    strPhone = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
			    SimpleDateFormat dateFormat = new SimpleDateFormat(   
                        "yyyy-MM-dd hh:mm:ss");   
                Date d = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(Calls.DATE))));   
                date = dateFormat.format(d); 
                
                Message msg = mHandler.obtainMessage(1);  
                Bundle b = new Bundle();// 存放数据  
                b.putString("phone", strPhone);
                b.putString("date", date);
                b.putLong("time", duration);
                msg.setData(b);  
                msg.sendToTarget(); 
			    
			    switch (type) {  
			        case Calls.INCOMING_TYPE:  
			            incoming += duration;  
			            break;  
			        case Calls.OUTGOING_TYPE:  
			            outgoing += duration;  
			        default:  
			            break;  
			    }  
			    count++;  
			    hasRecord = cursor.moveToNext();  
			}  		
	}
           

三、通过handler去解析内容并输出日志

private Handler mHandler = new Handler() {  
		  @Override  
		  public void handleMessage(Message msg) {  
		    // TODO Auto-generated method stub  
		    switch(msg.what){  
		    case 0:  
		    {  
		      Bundle b = msg.getData();  
		      String phone = b.getString("phone");
		      String smsbody = b.getString("smsbody");
		      String data = b.getString("data");
		      String type = b.getString("type");
		      Log.d("phone", phone);
		      Log.d("smsbody", smsbody);
		      Log.d("data", data);
		      Log.d("type", type);
		      
		    }  
		    break; 
		    case 1:
		    {
		    	Bundle b = msg.getData(); 
		    	String phone = b.getString("phone");
		    	String date = b.getString("date");
		    	long time = b.getLong("time");
		    	Log.d("phone", phone);
		    	Log.d("date", date);
		    	Log.d("time", ""+time);
		    }
		    break;
		   }  
		   super.handleMessage(msg);  
		   }  
		};  
           

四、由于搜索手机短信和通话信息是一项耗时的事情,所以需要启动一个线程去完成这些事情

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		new Thread()
		{
			public void run()
			{
				getSmsInPhone();
				GetCallsInPhone();
			}
		}.start();
	}
           

最后需要注意的是要启动访问权限

<uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
           

最后奉上源码下载地址: http://download.csdn.net/detail/m370809968/6639563