Api demo源码学习(8)--App/Activity/QuickContactsDemo --获取系统联系人信息

本节通过Content Provider机制获取系统中的联系人信息,注意这个Anctivity直接继承的是ListActivity,所以不再需要setContentView函数来加载布局文件了(我自己新建一个项目来跑这个anctivity时在这里卡了半天)。


在AndroidManifest.xml中需配置权限,以访问手机中的联系人信息,添加如下代码:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
 
具体解释放入代码中。
 1 public class QuickContactsDemo extends ListActivity {
2
3 //设置要从联系人数据库中要查找的数据
4 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
5 Contacts._ID, // 0
6 Contacts.DISPLAY_NAME, // 1
7 Contacts.STARRED, // 2
8 Contacts.TIMES_CONTACTED, // 3
9 Contacts.CONTACT_PRESENCE, // 4
10 Contacts.PHOTO_ID, // 5
11 Contacts.LOOKUP_KEY, // 6
12 Contacts.HAS_PHONE_NUMBER, // 7
13 };
14
15 static final int SUMMARY_ID_COLUMN_INDEX = 0;
16 static final int SUMMARY_NAME_COLUMN_INDEX = 1;
17 static final int SUMMARY_STARRED_COLUMN_INDEX = 2;
18 static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 3;
19 static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 4;
20 static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 5;
21 static final int SUMMARY_LOOKUP_KEY = 6;
22 static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 7;
23
24
25 @Override
26 public void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28
29 //设置通过uri要查询的语句
30 String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
31 + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
32 + Contacts.DISPLAY_NAME + " != '' ))";
33
34 //通过ContentResolver的query函数,传入联系人的URI :Contacts.CONTENT_URI查询所需信息,最后一个参数决定按照联系人的姓名进行降序排列
35 Cursor c =
36 getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
37 null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
38
39 //一个cursor使用完毕后需将其关闭,cursor.close()。如果不想自己管理cursor,
40 //可调用下面的startManagingCursor语句让系统自行管理,cursor会在程序结束时自动释放
41 startManagingCursor(c);
42
43 ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.quick_contacts, c);
44 setListAdapter(adapter);
45
46 }
47
48 private final class ContactListItemAdapter extends ResourceCursorAdapter {
49 public ContactListItemAdapter(Context context, int layout, Cursor c) {
50 super(context, layout, c);
51 }
52
53
54 //重写bindView方法,设置每个ListView内每一个view的值
55 @Override
56 public void bindView(View view, Context context, Cursor cursor) {
57 final ContactListItemCache cache = (ContactListItemCache) view.getTag();
58 TextView nameView = cache.nameView;
59 QuickContactBadge photoView = cache.photoView;
60 // Set the name
61 cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer);
62 int size = cache.nameBuffer.sizeCopied;
63 cache.nameView.setText(cache.nameBuffer.data, 0, size);
64 final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
65 final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);
66 cache.photoView.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));
67 }
68
69 @Override
70 public View newView(Context context, Cursor cursor, ViewGroup parent) {
71 View view = super.newView(context, cursor, parent);
72 ContactListItemCache cache = new ContactListItemCache();
73 cache.nameView = (TextView) view.findViewById(R.id.name);
74 cache.photoView = (QuickContactBadge) view.findViewById(R.id.badge);
75 view.setTag(cache);
76
77 return view;
78 }
79 }
80
81 final static class ContactListItemCache {
82 public TextView nameView;
83 public QuickContactBadge photoView;
84 public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
85 }
86 }

quick_contacts.xml布局文件:

 1 <RelativeLayout
2 xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:paddingLeft="0dip"
5 android:paddingRight="9dip"
6 android:layout_height= "wrap_content"
7 android:minHeight="48dip">
8
9 <QuickContactBadge
10 android:id="@+id/badge"
11 android:layout_marginLeft="2dip"
12 android:layout_marginRight="14dip"
13 android:layout_marginTop="4dip"
14 android:layout_marginBottom="3dip"
15 android:layout_alignParentLeft="true"
16 android:layout_alignParentTop="true"
17 android:layout_height= "wrap_content"
18 android:layout_width= "wrap_content"
19 android:src="@drawable/ic_contact_picture"
20 style="?android:attr/quickContactBadgeStyleWindowSmall" />
21
22 <TextView
23 android:id="@+id/name"
24 android:textAppearance="?android:attr/textAppearanceMedium"
25 android:paddingLeft="2dip"
26 android:layout_centerVertical="true"
27 android:layout_toRightOf="@id/badge"
28 android:layout_width="fill_parent"
29 android:layout_height="wrap_content" />
30
31 </RelativeLayout>
以上即可。




posted @ 2011-12-14 20:24  双重否定  阅读(413)  评论(0编辑  收藏  举报