之前稍微研究了如何創(chuàng)建并簡單使用ContentPRovider來實現共享數據功能。今天補充一下系統(tǒng)提供的ContentProvider是怎么一回事.。當然都是一樣的原理,最重要的就是所提供的的網址不同,也就是uri不同。系統(tǒng)中數據無外乎就是一些聯(lián)系人,信息等,這里在網上找到一些關于聯(lián)系人與短信的uri,在這里看一下:
我們能看到在微信或者QQ上有通過訪問手機上聯(lián)系人來添加好友,就相當于直接在手機這個系統(tǒng)中獲取數據。今天研究了下,發(fā)現了它們有兩種展現方式。
一是直接進入聯(lián)系人列表然后將所選聯(lián)系人數據帶過來,二呢是將聯(lián)系人列表里的數據綁進自己的頁面。哪個更好呢,當然就視情境而定了。只是哪個更簡單
到是可以探究一下。
先說第一種:
用一個ListView 裝數據,首先得寫一個自定義的適配器ContactListAdapter,聯(lián)系人儲存在手機這個數據庫中,數據字段繁多,我們要想取它得需要為它創(chuàng)建個實體類,接下來也就上次獲取數據一樣的步驟,具體看代碼
//綁定聯(lián)系人點擊事件public void getAllContacts(View view){ setAdapter(list);}/** * 初始化數據庫查詢參數 */private void init() { Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // 聯(lián)系人Uri; // 查詢的字段 String[] projection = { ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key", ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.PHOTO_ID, ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY }; // 按照sort_key升序查詢 asyncQueryHandler.startQuery(0, null, uri, projection, null, null, "sort_key COLLATE LOCALIZED asc");}/** * * @author Administrator * */private class MyAsyncQueryHandler extends AsyncQueryHandler { public MyAsyncQueryHandler(ContentResolver cr) { super(cr); } @Override protected void onQueryComplete(int token, Object cookie, Cursor cursor) { if (cursor != null && cursor.getCount() > 0) { contactIdMap = new HashMap<Integer, ContactBean>(); list = new ArrayList<ContactBean>(); cursor.moveToFirst(); // 游標移動到第一項 for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToPosition(i); String name = cursor.getString(1); String number = cursor.getString(2); String sortKey = cursor.getString(3); int contactId = cursor.getInt(4); Long photoId = cursor.getLong(5); String lookUpKey = cursor.getString(6); if (contactIdMap.containsKey(contactId)) { // 無操作 } else { // 創(chuàng)建聯(lián)系人對象 ContactBean contact = new ContactBean(); contact.setDesplayName(name); contact.setPhoneNum(number); contact.setSortKey(sortKey); contact.setPhotoId(photoId); contact.setLookUpKey(lookUpKey); Log.i("ccc",photoId+""); list.add(contact); contactIdMap.put(contactId, contact); } } } super.onQueryComplete(token, cookie, cursor); }}private void setAdapter(final List<ContactBean> list) { adapter = new ContactListAdapter(this, list); contactList.setAdapter(adapter);} |
第二種:
在這里我們就不需要用ListView來裝數據,只需用一個文本框接收從聯(lián)系人列表傳過來的值就ok了。那么,當然我們也不需要再自定義一個適配器,似乎相對來
說這一種方法簡單一點了
//跳到聯(lián)系人列表點擊事件public void jumpContacts(View view){ Uri uri=Uri.parse("content://contacts/people"); Intent intent=new Intent(Intent.ACTION_PICK,uri); startActivityForResult(intent,0);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case 0: if(data==null) { return; } //處理返回的data,獲取選擇的聯(lián)系人信息 Uri uri=data.getData(); String[] contacts=getPhoneContacts(uri); s = contacts[0]+":"+contacts[1]; tv_test_contacts.setText(s); break; } super.onActivityResult(requestCode, resultCode, data);}private String[] getPhoneContacts(Uri uri){ String[] contact=new String[2]; //得到ContentResolver對象 ContentResolver cr = getContentResolver(); //取得電話本中開始一項的光標 Cursor cursor=cr.query(uri,null,null,null,null); if(cursor!=null) { cursor.moveToFirst(); //取得聯(lián)系人姓名 int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); contact[0]=cursor.getString(nameFieldColumnIndex); //取得電話號碼 String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null); if(phone != null){ phone.moveToFirst(); contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phone.close(); cursor.close(); } else { return null; } return contact;} |
通過代碼量來看,好像第二種方法較簡單了一點,少了將手機數據庫中所有的數據獲取并綁定出來的這一步,當然早就說了視情境而定,各人有各人的看法了。
新聞熱點
疑難解答