• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            life02

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              197 隨筆 :: 3 文章 :: 37 評論 :: 0 Trackbacks

            http://zymic.iteye.com/blog/737643android中ContactsContract獲取聯系人的方法
            從Android 2.0 SDK開始有關聯系人provider的類變成了ContactsContract,雖然老的android.provider.Contacts能用,但是在SDK中標記為為deprecated將被放棄不推薦的方法,而從Android 2.0及API Level為5開始新增了android.provider.ContactsContract來代替原來的方法。

            ContactsContract的子類ContactsContract.Contacts是一張表,代表了所有聯系人的統計信息。比如聯系人ID(—ID),查詢鍵(LOOKUP_KEY),聯系人的姓名(DISPLAY_NAME_PRIMARY),頭像的id(PHOTO_ID)以及群組的id等等。

            我們可以通過以下的方法取得所有聯系人的表的Cursor對象:

            1)ContentResolver contentResolver=getContentResolver();//獲取 ContentResolver對象查詢在ContentProvider里定義的共享對象;

            2)Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);//根據URI對象ContactsContract.Contacts.CONTENT_URI查詢所有聯系人;

            從Cursor對象里我們關鍵是要取得聯系人的_id。通過它,再通過ContactsContract.CommonDataKinds的各個子類查詢該_id聯系人的電話(ContactsContract.CommonDataKinds.Phone),email(ContactsContract.CommonDataKinds.Email)等等。

            以取得該聯系人所有電話為例:

            1)int idFieldIndex=cursor.getColumnIndex(ContactsContract.Contacts._ID);

                         int id=cursor.getInt(idFieldIndex);//根據列名取得該聯系人的id;

            2)Cursor phonecursor=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{Integer.toString(id)}, null);//再類ContactsContract.CommonDataKinds.Phone中根據查詢相應id聯系人的所有電話;

            類似地可以ContactsContract.CommonDataKinds的不同的子類查詢不同的內容。android文檔告訴我們推薦使用ContactsContract.Contacts.LOOKUP_KEY代替ContactsContract.Contacts._ID。

            最后,由于讀取聯系人比較的占用資源,為了提高用戶的體驗度。考慮將讀取的過程放在線程里完成,推薦使用AsyncTask類。

             1package jtapp.contacts;
             2
             3import java.util.ArrayList;
             4import java.util.HashMap;
             5import java.util.List;
             6
             7import android.app.ListActivity;
             8import android.database.Cursor;
             9import android.os.Bundle;
            10import android.provider.ContactsContract;
            11import android.widget.SimpleAdapter;
            12
            13public class ViewContacts extends ListActivity {
            14    /** Called when the activity is first created. */
            15    @Override
            16    public void onCreate(Bundle savedInstanceState) {
            17        super.onCreate(savedInstanceState);
            18        setContentView(R.layout.main);
            19        
            20        List<HashMap<String, String>> items = fillMaps();        
            21        SimpleAdapter adapter = new SimpleAdapter(
            22                        this,items,R.layout.list_item,
            23                        new String[]{"name","key"}
            24                        new int[]{R.id.item,R.id.item2});
            25        this.setListAdapter(adapter);
            26
            27    }

            28
            29        private List<HashMap<String, String>> fillMaps() {
            30                List<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
            31
            32                Cursor cur = null;
            33                try {
            34                        // Query using ContentResolver.query or Activity.managedQuery
            35                        cur = getContentResolver().query(
            36                                        ContactsContract.Contacts.CONTENT_URI, nullnullnullnull);
            37                        if (cur.moveToFirst()) {
            38                                int idColumn = cur.getColumnIndex(
            39                                                ContactsContract.Contacts._ID);
            40                        int displayNameColumn = cur.getColumnIndex(
            41                                        ContactsContract.Contacts.DISPLAY_NAME);
            42                                // Iterate all users
            43                        do {
            44                                        String contactId;
            45                                        String displayName;
            46                                        String phoneNumber = "";
            47                                        // Get the field values
            48                                        contactId = cur.getString(idColumn);
            49                                        displayName = cur.getString(displayNameColumn);
            50                                        // Get number of user's phoneNumbers
            51                                        int numberCount = cur.getInt(cur.getColumnIndex(
            52                                                        ContactsContract.Contacts.HAS_PHONE_NUMBER));
            53                                        if (numberCount>0{
            54                                                Cursor phones = getContentResolver().query(
            55                                                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            56                                                                null,
            57                                                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
            58                                                                + " = " + contactId 
            59                                                                /*+ " and " + ContactsContract.CommonDataKinds.Phone.TYPE 
            60                                                                + "=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE*/
            ,
            61                                                                nullnull);
            62                                                if (phones.moveToFirst()) {
            63                                                        int numberColumn = phones.getColumnIndex(
            64                                                                        ContactsContract.CommonDataKinds.Phone.NUMBER);
            65                                                        // Iterate all numbers
            66                                                        do {
            67                                                                phoneNumber += phones.getString(numberColumn) + ",";
            68                                                        }
             while (phones.moveToNext());
            69                                                }
             
            70                                        }

            71                                        // Add values to items
            72                                        HashMap<String, String> i = new HashMap<String, String>();
            73                                        i.put("name", displayName);
            74                                        i.put("key", phoneNumber);
            75                                        items.add(i);
            76                                }
             while (cur.moveToNext());
            77                        }
             else {
            78                                HashMap<String, String> i = new HashMap<String, String>();
            79                                i.put("name""Your Phone");
            80                                i.put("key""Have No Contacts.");
            81                                items.add(i);
            82                        }

            83                }
             finally {
            84                        if (cur != null)
            85                                cur.close();
            86                }

            87                return items;
            88        }

            89}


            posted on 2012-01-18 16:34 life02 閱讀(725) 評論(0)  編輯 收藏 引用 所屬分類: Android開發
            久久精品国产亚洲5555| 中文字幕日本人妻久久久免费| 久久99国产精品尤物| 国产成人久久AV免费| 国产精品激情综合久久| 久久国产精品免费一区| 久久久久亚洲av综合波多野结衣| 奇米综合四色77777久久| 91亚洲国产成人久久精品网址| 日日狠狠久久偷偷色综合免费 | 久久中文字幕一区二区| 国产一区二区精品久久凹凸| 欧美成人免费观看久久| 久久99国产精一区二区三区| 久久婷婷色香五月综合激情| 久久精品国产久精国产| 午夜天堂av天堂久久久| 亚洲人成无码www久久久| 久久99精品国产99久久6男男| 久久伊人精品一区二区三区| 久久精品国产亚洲麻豆| 中文国产成人精品久久不卡| 欧美久久久久久精选9999| 精品国产一区二区三区久久久狼| 久久综合九色综合欧美就去吻| 久久不射电影网| 少妇久久久久久久久久| 97精品依人久久久大香线蕉97| 久久午夜福利电影| 蜜臀久久99精品久久久久久| 99久久精品免费| 精品一久久香蕉国产线看播放| 久久成人国产精品二三区| AV狠狠色丁香婷婷综合久久| 久久精品人人做人人爽电影蜜月| 精品一二三区久久aaa片| 久久这里的只有是精品23| 一本色道久久88—综合亚洲精品| 四虎影视久久久免费| 99久久精品免费| 久久99热这里只频精品6|