Android获取手机短信和通话记录及通讯录

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

一、获取手机短信信息

[java]  view plain  copy
 print ?
  1. /** 
  2.      * 获取所有短信 
  3.      *  
  4.      * @return 
  5.      */  
  6.     public String getSmsInPhone() {  
  7.         final String SMS_URI_ALL = "content://sms/";  
  8.   
  9.         StringBuilder smsBuilder = new StringBuilder();  
  10.   
  11.         try {  
  12.             Uri uri = Uri.parse(SMS_URI_ALL);  
  13.             String[] projection = new String[] { "_id""address""person",  
  14.                     "body""date""type" };  
  15.             Cursor cur = getContentResolver().query(uri, projection, null,  
  16.                     null"date desc"); // 获取手机内部短信  
  17.   
  18.             if (cur.moveToFirst()) {  
  19.                 int index_Address = cur.getColumnIndex("address");  
  20.                 int index_Person = cur.getColumnIndex("person");  
  21.                 int index_Body = cur.getColumnIndex("body");  
  22.                 int index_Date = cur.getColumnIndex("date");  
  23.                 int index_Type = cur.getColumnIndex("type");  
  24.   
  25.                 do {  
  26.                     String strAddress = cur.getString(index_Address);  
  27.                     int intPerson = cur.getInt(index_Person);  
  28.                     String strbody = cur.getString(index_Body);  
  29.                     long longDate = cur.getLong(index_Date);  
  30.                     int intType = cur.getInt(index_Type);  
  31.   
  32.                     SimpleDateFormat dateFormat = new SimpleDateFormat(  
  33.                             "yyyy-MM-dd hh:mm:ss");  
  34.                     Date d = new Date(longDate);  
  35.                     String strDate = dateFormat.format(d);  
  36.   
  37.                     String strType = "";  
  38.                     if (intType == 1) {  
  39.                         strType = "接收";  
  40.                     } else if (intType == 2) {  
  41.                         strType = "发送";  
  42.                     } else {  
  43.                         strType = "null";  
  44.                     }  
  45.   
  46.                     smsBuilder.append("[ ");  
  47.                     smsBuilder.append(strAddress + ", ");  
  48.                     smsBuilder.append(intPerson + ", ");  
  49.                     smsBuilder.append(strbody + ", ");  
  50.                     smsBuilder.append(strDate + ", ");  
  51.                     smsBuilder.append(strType);  
  52.                     smsBuilder.append(" ]\n\n");  
  53.                 } while (cur.moveToNext());  
  54.   
  55.                 if (!cur.isClosed()) {  
  56.                     cur.close();  
  57.                     cur = null;  
  58.                 }  
  59.             } else {  
  60.                 smsBuilder.append("no result!");  
  61.             } // end if  
  62.   
  63.             smsBuilder.append("getSmsInPhone has executed!");  
  64.   
  65.         } catch (SQLiteException ex) {  
  66.             Log.d("SQLiteException in getSmsInPhone", ex.getMessage());  
  67.         }  
  68.   
  69.         return smsBuilder.toString();  
  70.     }  

二、获取通话记录


[java]  view plain  copy
 print ?
  1. /** 
  2.      * 获取通话记录 
  3.      */  
  4.     private void GetCallsInPhone() {  
  5.         String result = null;  
  6.         Cursor cursor = getContentResolver().query(  
  7.                 Calls.CONTENT_URI,  
  8.                 new String[] { Calls.DURATION, Calls.TYPE, Calls.DATE,  
  9.                         Calls.NUMBER }, nullnull, Calls.DEFAULT_SORT_ORDER);  
  10.         boolean hasRecord = cursor.moveToFirst();  
  11.         int count = 0;  
  12.         String strPhone = "";  
  13.         String date;  
  14.   
  15.         while (hasRecord) {  
  16.             int type = cursor.getInt(cursor.getColumnIndex(Calls.TYPE));  
  17.             long duration = cursor.getLong(cursor  
  18.                     .getColumnIndex(Calls.DURATION));  
  19.             strPhone = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));  
  20.             SimpleDateFormat dateFormat = new SimpleDateFormat(  
  21.                     "yyyy-MM-dd hh:mm:ss");  
  22.             Date d = new Date(Long.parseLong(cursor.getString(cursor  
  23.                     .getColumnIndex(Calls.DATE))));  
  24.             date = dateFormat.format(d);  
  25.   
  26.             result = result + "phone :" + strPhone + ",";  
  27.   
  28.             result = result + "date :" + date + ",";  
  29.             result = result + "time :" + duration + ",";  
  30.   
  31.             switch (type) {  
  32.             case Calls.INCOMING_TYPE:  
  33.                 result = result + "type :呼入";  
  34.                 break;  
  35.             case Calls.OUTGOING_TYPE:  
  36.                 result = result + "type :呼出";  
  37.             default:  
  38.                 break;  
  39.             }  
  40.             result += "\n";  
  41.             count++;  
  42.             hasRecord = cursor.moveToNext();  
  43.         }  
  44.         Log.i(TAG, result);  
  45.   
  46.         textView.setText(result);  
  47.     }  

三、获取联系人

[java]  view plain  copy
 print ?
  1. /** 
  2.  * 获取联系人 
  3.  */  
  4. private void getContact2() {  
  5.     String string = "";  
  6.     int count = 0;  
  7.     ContentResolver resolver = getApplicationContext().getContentResolver();  
  8.   
  9.     Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,  
  10.             nullnullnullnull);  
  11.   
  12.     while (cursor.moveToNext()) {  
  13.   
  14.         // 取得联系人的名字索引  
  15.         int nameIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
  16.         String contact = cursor.getString(nameIndex);  
  17.         string += contact + ":";  
  18.         // 取得联系人的ID索引值  
  19.         String contactId = cursor.getString(cursor  
  20.                 .getColumnIndex(ContactsContract.Contacts._ID));  
  21.   
  22.         // 查询该位联系人的电话号码,类似的可以查询email,photo  
  23.         Cursor phone = resolver.query(  
  24.                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,  
  25.                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "  
  26.                         + contactId, nullnull);// 第一个参数是确定查询电话号,第三个参数是查询具体某个人的过滤值  
  27.   
  28.         // 一个人可能有几个号码  
  29.         while (phone.moveToNext()) {  
  30.             String strPhoneNumber = phone  
  31.                     .getString(phone  
  32.                             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
  33.             string = string + strPhoneNumber + " ;";  
  34.   
  35.         }  
  36.   
  37.         string += "\n";  
  38.         count++;  
  39.         phone.close();  
  40.     }  
  41.     cursor.close();  
  42.     Log.i(TAG, String.valueOf(count));  
  43.     // 设置显示内容  
  44.     textView.setText(string);  
  45.   
  46.   
  47. }  
最后需要注意的是要启动访问权限

[html]  view plain  copy
 print ?
  1. <uses-permission android:name="android.permission.READ_SMS" />  
  2.  <uses-permission android:name="android.permission.READ_CONTACTS" />  
  3.  <uses-permission android:name="android.permission.READ_CALL_LOG" /> 
源码下载地址 :http://download.csdn.net/detail/leokelly001/8129689
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值