国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ContentProvider補(bǔ)充(獲取系統(tǒng)數(shù)據(jù))

2019-11-10 17:19:16
字體:
供稿:網(wǎng)友

              之前稍微研究了如何創(chuàng)建并簡單使用ContentPRovider來實(shí)現(xiàn)共享數(shù)據(jù)功能。今天補(bǔ)充一下系統(tǒng)提供的ContentProvider是怎么一回事.。當(dāng)然都是一樣的原理,最重要的就是所提供的的網(wǎng)址不同,也就是uri不同。系統(tǒng)中數(shù)據(jù)無外乎就是一些聯(lián)系人,信息等,這里在網(wǎng)上找到一些關(guān)于聯(lián)系人與短信的uri,在這里看一下:

     

我們能看到在微信或者QQ上有通過訪問手機(jī)上聯(lián)系人來添加好友,就相當(dāng)于直接在手機(jī)這個(gè)系統(tǒng)中獲取數(shù)據(jù)。今天研究了下,發(fā)現(xiàn)了它們有兩種展現(xiàn)方式。

一是直接進(jìn)入聯(lián)系人列表然后將所選聯(lián)系人數(shù)據(jù)帶過來,二呢是將聯(lián)系人列表里的數(shù)據(jù)綁進(jìn)自己的頁面。哪個(gè)更好呢,當(dāng)然就視情境而定了。只是哪個(gè)更簡單

到是可以探究一下。

先說第一種:

         用一個(gè)ListView 裝數(shù)據(jù),首先得寫一個(gè)自定義的適配器ContactListAdapter,聯(lián)系人儲(chǔ)存在手機(jī)這個(gè)數(shù)據(jù)庫中,數(shù)據(jù)字段繁多,我們要想取它得需要為它創(chuàng)建個(gè)實(shí)體類,接下來也就上次獲取數(shù)據(jù)一樣的步驟,具體看代碼

//綁定聯(lián)系人點(diǎn)擊事件public void getAllContacts(View view){    setAdapter(list);}/** * 初始化數(shù)據(jù)庫查詢參數(shù) */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(); // 游標(biāo)移動(dòng)到第一項(xiàng)            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)系人對(duì)象                    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來裝數(shù)據(jù),只需用一個(gè)文本框接收從聯(lián)系人列表傳過來的值就ok了。那么,當(dāng)然我們也不需要再自定義一個(gè)適配器,似乎相對(duì)來

說這一種方法簡單一點(diǎn)了

//跳到聯(lián)系人列表點(diǎ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對(duì)象    ContentResolver cr = getContentResolver();    //取得電話本中開始一項(xiàng)的光標(biāo)    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);        //取得電話號(hào)碼        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;}

通過代碼量來看,好像第二種方法較簡單了一點(diǎn),少了將手機(jī)數(shù)據(jù)庫中所有的數(shù)據(jù)獲取并綁定出來的這一步,當(dāng)然早就說了視情境而定,各人有各人的看法了。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿拉善左旗| 图木舒克市| 闻喜县| 临沧市| 喀喇沁旗| 宝丰县| 文昌市| 西藏| 宁晋县| 桦南县| 灵山县| 鹿泉市| 古交市| 乌审旗| 通道| 珠海市| 邢台市| 桓仁| 修文县| 明溪县| 乌什县| 湖州市| 萨嘎县| 如东县| 东安县| 德兴市| 洛宁县| 东莞市| 鹤峰县| 诸城市| 东宁县| 呼和浩特市| 涟源市| 兴和县| 平乐县| 华亭县| 正镶白旗| 蒙城县| 平远县| 长武县| 尼勒克县|