http://zymic.iteye.com/blog/737643android中ContactsContract获取联系人的方法
从Android 2.0 SDK开始有关联系人provider的类变成了ContactsContract,虽然老的android.provider.Contacts能用,但是在SDK中标记为为deprecated将被放弃不推荐的方法,而从Android 2.0及API Level为5开始新增了android.provider.ContactsContract来代替原来的方法。
ContactsContract的子类ContactsContract.Contacts是一张表,代表了所有联系人的统计信息。比如联系人ID(—ID),查询键(LOOKUP_KEY),联系人的姓名(DISPLAY_NAME_PRIMARY),头像的id(PHOTO_ID)以及群组的id等等。
我们可以通过以下的方法取得所有联系人的表的Cursor对象:
1)ContentResolver contentResolver=getContentResolver();//获取 ContentResolver对象查询在ContentProvider里定义的共享对象;
2)Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);//根据URI对象ContactsContract.Contacts.CONTENT_URI查询所有联系人;
从Cursor对象里我们关键是要取得联系人的_id。通过它,再通过ContactsContract.CommonDataKinds的各个子类查询该_id联系人的电话(ContactsContract.CommonDataKinds.Phone),email(ContactsContract.CommonDataKinds.Email)等等。
以取得该联系人所有电话为例:
1)int idFieldIndex=cursor.getColumnIndex(ContactsContract.Contacts._ID);
int id=cursor.getInt(idFieldIndex);//根据列名取得该联系人的id;
2)Cursor phonecursor=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{Integer.toString(id)}, null);//再类ContactsContract.CommonDataKinds.Phone中根据查询相应id联系人的所有电话;
类似地可以ContactsContract.CommonDataKinds的不同的子类查询不同的内容。android文档告诉我们推荐使用ContactsContract.Contacts.LOOKUP_KEY代替ContactsContract.Contacts._ID。
最后,由于读取联系人比较的占用资源,为了提高用户的体验度。考虑将读取的过程放在线程里完成,推荐使用AsyncTask类。
1
package jtapp.contacts;
2
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.List;
6
7
import android.app.ListActivity;
8
import android.database.Cursor;
9
import android.os.Bundle;
10
import android.provider.ContactsContract;
11
import android.widget.SimpleAdapter;
12
13
public class ViewContacts extends ListActivity
{
14
/** *//** Called when the activity is first created. */
15
@Override
16
public void onCreate(Bundle savedInstanceState)
{
17
super.onCreate(savedInstanceState);
18
setContentView(R.layout.main);
19
20
List<HashMap<String, String>> items = fillMaps();
21
SimpleAdapter adapter = new SimpleAdapter(
22
this,items,R.layout.list_item,
23
new String[]
{"name","key"},
24
new int[]
{R.id.item,R.id.item2});
25
this.setListAdapter(adapter);
26
27
}
28
29
private List<HashMap<String, String>> fillMaps()
{
30
List<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
31
32
Cursor cur = null;
33
try
{
34
// Query using ContentResolver.query or Activity.managedQuery
35
cur = getContentResolver().query(
36
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
37
if (cur.moveToFirst())
{
38
int idColumn = cur.getColumnIndex(
39
ContactsContract.Contacts._ID);
40
int displayNameColumn = cur.getColumnIndex(
41
ContactsContract.Contacts.DISPLAY_NAME);
42
// Iterate all users
43
do
{
44
String contactId;
45
String displayName;
46
String phoneNumber = "";
47
// Get the field values
48
contactId = cur.getString(idColumn);
49
displayName = cur.getString(displayNameColumn);
50
// Get number of user's phoneNumbers
51
int numberCount = cur.getInt(cur.getColumnIndex(
52
ContactsContract.Contacts.HAS_PHONE_NUMBER));
53
if (numberCount>0)
{
54
Cursor phones = getContentResolver().query(
55
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
56
null,
57
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
58
+ " = " + contactId
59
/**//*+ " and " + ContactsContract.CommonDataKinds.Phone.TYPE
60
+ "=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE*/,
61
null, null);
62
if (phones.moveToFirst())
{
63
int numberColumn = phones.getColumnIndex(
64
ContactsContract.CommonDataKinds.Phone.NUMBER);
65
// Iterate all numbers
66
do
{
67
phoneNumber += phones.getString(numberColumn) + ",";
68
} while (phones.moveToNext());
69
}
70
}
71
// Add values to items
72
HashMap<String, String> i = new HashMap<String, String>();
73
i.put("name", displayName);
74
i.put("key", phoneNumber);
75
items.add(i);
76
} while (cur.moveToNext());
77
} else
{
78
HashMap<String, String> i = new HashMap<String, String>();
79
i.put("name", "Your Phone");
80
i.put("key", "Have No Contacts.");
81
items.add(i);
82
}
83
} finally
{
84
if (cur != null)
85
cur.close();
86
}
87
return items;
88
}
89
}