• <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 閱讀(722) 評論(0)  編輯 收藏 引用 所屬分類: Android開發
            99精品久久久久久久婷婷| 一本久久精品一区二区| 免费无码国产欧美久久18| 精品久久亚洲中文无码| 精品永久久福利一区二区| 久久夜色tv网站| 久久91精品国产91久| 99久久无色码中文字幕人妻| 久久精品中文字幕久久| 久久天天躁狠狠躁夜夜av浪潮| 久久99国产精品尤物| 久久久久亚洲精品无码网址 | 久久综合久久综合久久| 久久男人中文字幕资源站| 久久精品国产亚洲av麻豆小说| 99久久香蕉国产线看观香| 996久久国产精品线观看| 久久综合久久自在自线精品自| 久久综合亚洲鲁鲁五月天| 久久亚洲高清观看| 久久香综合精品久久伊人| 久久夜色撩人精品国产小说| 精品久久久久久久久中文字幕| 精品无码久久久久久午夜| 久久精品免费全国观看国产| 午夜精品久久影院蜜桃| 国色天香久久久久久久小说| 国产高潮国产高潮久久久91| 精品久久人人爽天天玩人人妻| 久久综合色区| 久久综合狠狠综合久久激情 | 久久精品水蜜桃av综合天堂| 色欲综合久久躁天天躁蜜桃| 丰满少妇人妻久久久久久| 久久亚洲精品无码AV红樱桃| 久久久国产打桩机| 中文字幕人妻色偷偷久久| 伊人久久无码中文字幕| 亚洲AV无码久久精品蜜桃| 麻豆AV一区二区三区久久| 91精品国产9l久久久久|