標籤:apidemo contacts badge 內容提供者 內容解析者
這個demeo主要示範的是使內容提供者和內容解析者來擷取其它應用的資料。Content Provider為不同應用之間共用資料提供了統一的介面,Android系統的每個Content Provider都定義了一個CONTENT_URI,Android中每個Context對象(如Activity)對含有一個ContentResolver,ContentResolver可以根據CONTENT_URI擷取對應的Content Provider。
在本例中使用了startManagingCursor(c);對cursor進行託管,但在android3.0以後推出了更為安全和靈活的cursor管理方式,即使用LoaderManager,本例就是在原有的基礎上進行了改造,使用LoaderManager來完成這個demo。
在本例中讓ManiActivity繼承了ListActivity,所以需要去掉setContentView(),不然會報錯。
activity_main.xml只做為了listActivity的每個條目的布局。在這裡使用了QuickContactBadge組件,該組件繼承ImageView,用於顯示連絡人左側的表徵圖。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="5dp"> <QuickContactBadge android:id="@+id/badge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@drawable/ic_launcher" style="?android:attr/quickContactBadgeStyleSmallWindowSmall"/> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_marginLeft="10dp" android:layout_gravity="center_vertical" /></LinearLayout>
MainActivity:這裡使用了ResourceCursorAdapter的子類作為listActivity的適配器。
public class MainActivity extends ListActivity { // 定義需要從連絡人中取出的列 private final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID,// 0 Contacts.DISPLAY_NAME,// 1 Contacts.STARRED,// 2 Contacts.TIMES_CONTACTED,// 3 Contacts.CONTACT_PRESENCE,// 4 Contacts.PHOTO_ID,// 5 Contacts.LOOKUP_KEY,// 6 Contacts.HAS_PHONE_NUMBER // 7 }; private static final int SUMMARY_ID_COLUMN_INDEX = 0; private static final int SUMMARY_NAME_COLUMN_INDEX = 1; private static final int SUMMARY_STARRED_COLUMN_INDEX = 2; private static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 3; private static final int SUMMARY_CONTACT_PRESENCE_COLUMN_INDEX = 4; private static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 5; private static final int SUMMARY_LOOKUP_KEY_COLUMN_INDEX = 6; private static final int SUMMARY_HAS_PHONE_NUMBER_COLUMN_INDEX = 7; private static final int LOADER_ID = 1; private String selection; private ContactListItemAdapter adapter; /** * android 3.0以後提供了更加安全和靈活的對資料進行非同步載入的方式,就是使用LoaderManager * 通過它可以輕鬆地實現對cursor的管理 */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); selection = "((" + Contacts.DISPLAY_NAME + " NOTNULL) And (" + Contacts.DISPLAY_NAME + "!=‘‘) And (" + Contacts.HAS_PHONE_NUMBER + "=1))"; // flags 這個標誌用來決定該適配器的行為。(Android3.0推薦我們傳遞 // CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER。設定標誌用來添加一個 // 監聽器,監聽著參數cursor的資料是否有更變。) adapter = new ContactListItemAdapter(MainActivity.this, R.layout.activity_main, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); setListAdapter(adapter); // 擷取LoaderManager LoaderManager manager = getLoaderManager(); // 初始化loader對象 manager.initLoader(LOADER_ID, null, MyLoader); //將遊標交給activity託管 startManagingCursor(c); } private LoaderCallbacks<Cursor> MyLoader = new LoaderCallbacks<Cursor>() { // 建立一個loader對象,該對象攜帶遊標 @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { // 建立cursorloader對象第二個參數為內容提供者的Uri,第三個參數為 // 要查詢出哪些列,第四個參數指定查詢的條件,第五個參數為查詢的參數, // 最後一個參數表示 按本地語言的名字降序進行排序 CursorLoader cl = new CursorLoader(MainActivity.this, Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, selection, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); return cl; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { if (loader.getId() == LOADER_ID) { if (cursor == null) { Toast.makeText(MainActivity.this, "查詢失敗!", 0).show(); return; } if (cursor.getCount() == 0) { Toast.makeText(MainActivity.this, "查詢結果為空白!", 0).show(); } // 更新UI資訊 /* * 使用內容解析者根據連絡人的URI對連絡人進行查詢,第一個參數為連絡人Uri,第二個參數為 * 要查詢出哪些列,第三個參數指定查詢的條件,第四個參數為查詢的參數,最後一個參數表示 按本地語言的名字降序進行排序 */ Cursor c = getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, selection, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); adapter.swapCursor(c); } } @Override public void onLoaderReset(Loader<Cursor> loader) { Log.i("TAG", "loader was reseted"); } }; private class ContactListItemAdapter extends ResourceCursorAdapter { public ContactListItemAdapter(Context context, int layout, Cursor c, int flags) { super(context, layout, c, flags); } /** * 找到各個組件 */ @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = super.newView(context, cursor, parent); ContactListItemCache cache = new ContactListItemCache(); cache.nameView = (TextView) view.findViewById(R.id.tv_name); cache.badge = (QuickContactBadge) view.findViewById(R.id.badge); view.setTag(cache); return view; } /** * 為組件賦值 */ @Override public void bindView(View view, Context context, Cursor cursor) { ContactListItemCache cache = (ContactListItemCache) view.getTag(); // 將當前查詢到的名字複製到可變長度數組中 cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer); int size = cache.nameBuffer.sizeCopied; cache.nameView.setText(cache.nameBuffer.data, 0, size); // 為quickContactBadge賦值 long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX); String lookupKey = cursor .getString(SUMMARY_LOOKUP_KEY_COLUMN_INDEX); Uri lookupUri = Contacts.getLookupUri(contactId,lookupKey); //System.out.println("=====:"+lookupUri.toString()); // 指定和QuickContactBadge關聯的連絡人URI cache.badge.assignContactUri(lookupUri); } private class ContactListItemCache { public TextView nameView; public QuickContactBadge badge; // 構造一個可變長度的字元數組 public CharArrayBuffer nameBuffer = new CharArrayBuffer(128); } }}
最後就是需要在設定檔中設定讀取系統連絡人的許可權:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Android Api Demos登頂之路(十四)Quick Contacts