Api demo源码学习(8)--App/Activity/QuickContactsDemo --获取系统联系人信息
生活随笔
收集整理的這篇文章主要介紹了
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 <RelativeLayout2 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> 以上即可。
下一節:Api demo源碼學習(9)--App/Activity/Receive Result --Activity間傳遞數據
轉載于:https://www.cnblogs.com/xutao1988/archive/2011/12/14/2288027.html
總結
以上是生活随笔為你收集整理的Api demo源码学习(8)--App/Activity/QuickContactsDemo --获取系统联系人信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MFC学习之路之多媒体 --(1) Di
- 下一篇: mediawiki自动生成sitemap