(转)Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联

Android 的SMS读取短信,可以获取发信人/收信人的手机号码(address),Contacts的联系人,可以过滤手机号码(address),因此SMS可以通过手机号码(address)关联到Contacts联系人


sms是短信,mms是彩信,它们获取方式类似。把content://sms/修改为content://mms/即可,彩信里面的附件如图片、视频需要通过彩信的_id获取


SMS - Contacts 关联代码

[java]  view plain copy print ?
  1. // 通过address手机号关联Contacts联系人的显示名字  
  2.     private String getPeopleNameFromPerson(String address){  
  3.         if(address == null || address == ""){  
  4.             return "( no address )\n";  
  5.         }  
  6.           
  7.         String strPerson = "null";  
  8.         String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER};  
  9.           
  10.         Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address);  // address 手机号过滤  
  11.         Cursor cursor = getContentResolver().query(uri_Person, projection, nullnullnull);  
  12.           
  13.         if(cursor.moveToFirst()){  
  14.             int index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);  
  15.             String strPeopleName = cursor.getString(index_PeopleName);  
  16.             strPerson = strPeopleName;  
  17.         }  
  18.         cursor.close();  
  19.           
  20.         return strPerson;  
  21.     }  


SMS - Contacts 关联示例代码:

[java]  view plain copy print ?
  1. package com.homer.phone;  
  2.   
  3. import java.sql.Date;  
  4. import java.text.SimpleDateFormat;  
  5.   
  6. import android.app.Activity;  
  7. import android.database.Cursor;  
  8. import android.database.sqlite.SQLiteException;  
  9. import android.net.Uri;  
  10. import android.os.Bundle;  
  11. import android.provider.ContactsContract;  
  12. import android.provider.ContactsContract.CommonDataKinds.Phone;  
  13. import android.util.Log;  
  14. import android.widget.ScrollView;  
  15. import android.widget.TextView;  
  16.   
  17. public class phoneRead2 extends Activity {  
  18.   
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.   
  23.         TextView tv = new TextView(this);  
  24.         tv.setText(getSmsInPhone());  
  25.   
  26.         ScrollView sv = new ScrollView(this);  
  27.         sv.addView(tv);  
  28.           
  29.         setContentView(sv);  
  30.     }  
  31.   
  32.     public String getSmsInPhone() {  
  33.         final String SMS_URI_ALL = "content://sms/";  
  34.         final String SMS_URI_INBOX = "content://sms/inbox";  
  35.         final String SMS_URI_SEND = "content://sms/sent";  
  36.         final String SMS_URI_DRAFT = "content://sms/draft";  
  37.         final String SMS_URI_OUTBOX = "content://sms/outbox";  
  38.         final String SMS_URI_FAILED = "content://sms/failed";  
  39.         final String SMS_URI_QUEUED = "content://sms/queued";  
  40.   
  41.         StringBuilder smsBuilder = new StringBuilder();  
  42.   
  43.         try {  
  44.             Uri uri = Uri.parse(SMS_URI_ALL);  
  45.             String[] projection = new String[] { "_id""address""person""body""date""type" };  
  46.             Cursor cur = getContentResolver().query(uri, projection, nullnull"date desc");      // 获取手机内部短信  
  47.   
  48.             if (cur.moveToFirst()) {  
  49.                 int index_Address = cur.getColumnIndex("address");  
  50.                 int index_Person = cur.getColumnIndex("person");  
  51.                 int index_Body = cur.getColumnIndex("body");  
  52.                 int index_Date = cur.getColumnIndex("date");  
  53.                 int index_Type = cur.getColumnIndex("type");  
  54.   
  55.                 do {  
  56.                     String strAddress = cur.getString(index_Address);  
  57.                     int intPerson = cur.getInt(index_Person);  
  58.                     String strbody = cur.getString(index_Body);  
  59.                     long longDate = cur.getLong(index_Date);  
  60.                     int intType = cur.getInt(index_Type);  
  61.   
  62.                     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  63.                     Date d = new Date(longDate);  
  64.                     String strDate = dateFormat.format(d);  
  65.   
  66.                     String strType = "";  
  67.                     if (intType == 1) {  
  68.                         strType = "接收";  
  69.                     } else if (intType == 2) {  
  70.                         strType = "发送";  
  71.                     } else {  
  72.                         strType = "null";  
  73.                     }  
  74.   
  75.                     String strAddress2 = getPeopleNameFromPerson(strAddress);  
  76.                       
  77.                     smsBuilder.append("[ ");  
  78. //                  smsBuilder.append(strAddress + ", ");  
  79.                     smsBuilder.append(strAddress + " : " + strAddress2 + ", ");  
  80.                     smsBuilder.append(intPerson + ", ");  
  81.                     smsBuilder.append(strbody + ", ");  
  82.                     smsBuilder.append(strDate + ", ");  
  83.                     smsBuilder.append(strType);  
  84.                     smsBuilder.append(" ]\n\n");  
  85.                 } while (cur.moveToNext());  
  86.   
  87.                 if (!cur.isClosed()) {  
  88.                     cur.close();  
  89.                     cur = null;  
  90.                 }  
  91.             } else {  
  92.                 smsBuilder.append("no result!");  
  93.             } // end if  
  94.   
  95.             smsBuilder.append("getSmsInPhone has executed!");  
  96.   
  97.         } catch (SQLiteException ex) {  
  98.             Log.d("SQLiteException in getSmsInPhone", ex.getMessage());  
  99.         }  
  100.   
  101.         return smsBuilder.toString();  
  102.     }  
  103.       
  104.     // 通过address手机号关联Contacts联系人的显示名字  
  105.     private String getPeopleNameFromPerson(String address){  
  106.         if(address == null || address == ""){  
  107.             return "( no address )\n";  
  108.         }  
  109.           
  110.         String strPerson = "null";  
  111.         String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER};  
  112.           
  113.         Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address);  // address 手机号过滤  
  114.         Cursor cursor = getContentResolver().query(uri_Person, projection, nullnullnull);  
  115.           
  116.         if(cursor.moveToFirst()){  
  117.             int index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);  
  118.             String strPeopleName = cursor.getString(index_PeopleName);  
  119.             strPerson = strPeopleName;  
  120.         }  
  121.         cursor.close();  
  122.           
  123.         return strPerson;  
  124.     }  
  125.       
  126. }  

AndroidManifest.xml 权限

 记得在AndroidManifest.xml中加入android.permission.READ_SMSandroid.permission.READ_CONTACTS这两个permission



运行结果:



示例代码



参考推荐:

Android 之 Contacts 联系人读取


转自:http://blog.csdn.net/sunboy_2050/article/details/7328722

你可能感兴趣的:(Java/Android)