• <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開發
            久久艹国产| 72种姿势欧美久久久久大黄蕉| 午夜视频久久久久一区 | 日本福利片国产午夜久久| 久久无码国产| 国产精品久久久久久影院 | 亚洲精品乱码久久久久久不卡| 亚洲精品无码专区久久久| 久久精品国产免费| 国产亚洲精品久久久久秋霞 | 久久人人爽人人爽人人片AV不 | 99久久国产宗和精品1上映 | 久久久久久无码Av成人影院| 久久男人AV资源网站| 久久国产高潮流白浆免费观看| 久久久久黑人强伦姧人妻| 国产精品无码久久综合| 麻豆精品久久久久久久99蜜桃| 午夜不卡888久久| 国产成人久久精品激情 | 国产亚洲美女精品久久久久狼| 中文字幕精品无码久久久久久3D日动漫| 91精品国产乱码久久久久久| 色天使久久综合网天天| 性做久久久久久久久久久| 香蕉久久夜色精品国产小说| 久久青青草原亚洲av无码app| 国内精品伊人久久久影院 | 久久精品国产一区| 99久久精品毛片免费播放| 久久久久亚洲AV无码永不| 久久99精品久久久大学生| 久久人人爽人人爽人人片av麻烦| 无码8090精品久久一区 | 国产精品九九九久久九九| 亚洲综合伊人久久大杳蕉| 久久久久久综合网天天| 精品久久久久久中文字幕大豆网| 99久久综合国产精品免费| 久久亚洲精品无码aⅴ大香| 综合久久精品色|