现在的位置: 首页 > 综合 > 正文

Android Contacts(一)—— 读取联系人 Android Contacts(一)—— 读取联系人Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联

2017年12月12日 ⁄ 综合 ⁄ 共 9409字 ⁄ 字号 评论关闭

Android Contacts(一)—— 读取联系人

分类: Android 3509人阅读 评论(0) 收藏 举报

Introduction To Android Contacts

Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See the Android
SQLite and Cursor Article
 for more information. Google changed the contacts database moving from 1.x to 2.0 versions of Android. This tutorial will be broken into 3 sections. First covering accessing contacts in Android 2.0. The second page will deal with
accessing the contacts in Android 1.6 and before. Third we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records.

Contacts 读取代码:

  1. package com.homer.phone;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.database.Cursor;  
  8. import android.os.Bundle;  
  9. import android.provider.ContactsContract;  
  10. import android.provider.ContactsContract.CommonDataKinds.Phone;  
  11. import android.widget.ListView;  
  12. import android.widget.SimpleAdapter;  
  13.   
  14. public class phoneRead extends Activity {  
  15.         
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState){  
  18.         super.onCreate(savedInstanceState);  
  19.           
  20.         showListView();  
  21.     }  
  22.       
  23.     private void showListView(){  
  24.         ListView listView = new ListView(this);  
  25.           
  26.         ArrayList<HashMap<String, String>> list = getPeopleInPhone2();  
  27.         SimpleAdapter adapter = new SimpleAdapter(  
  28.                                     this,   
  29.                                     list,   
  30.                                     android.R.layout.simple_list_item_2,   
  31.                                     new String[] {"peopleName""phoneNum"},   
  32.                                     new int[]{android.R.id.text1, android.R.id.text2}  
  33.                                 );  
  34.         listView.setAdapter(adapter);  
  35.           
  36.         setContentView(listView);  
  37.     }  
  38.       
  39.     private ArrayList<HashMap<String, String>> getPeopleInPhone2(){  
  40.         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
  41.           
  42.         Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, nullnullnullnull);     // 获取手机联系人  
  43.         while (cursor.moveToNext()) {  
  44.             HashMap<String, String> map = new HashMap<String, String>();  
  45.               
  46.             int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);    // people name  
  47.             int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER);            // phone number  
  48.   
  49.             String strPeopleName = cursor.getString(indexPeopleName);  
  50.             String strPhoneNum = cursor.getString(indexPhoneNum);  
  51.   
  52.             map.put("peopleName", strPeopleName);  
  53.             map.put("phoneNum", strPhoneNum);  
  54.             list.add(map);  
  55.         }  
  56.         if(!cursor.isClosed()){  
  57.             cursor.close();  
  58.             cursor = null;  
  59.         }  
  60.           
  61.         return list;  
  62.     }  
  63. }  

AndroidManifest.xml 权限

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

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


运行结果:

代码示例

 

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

分类: Android 4019人阅读 评论(3) 收藏 举报

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

SMS - Contacts 关联代码

  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 关联示例代码:

  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

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

运行结果:

示例代码

抱歉!评论已关闭.