Android 開發工具類 37_ ContactInfoProvider

來源:互聯網
上載者:User

標籤:

Android 手機中的連絡人資訊儲存在  data\data\com.android.providers.contacts\databases\contacts2.db 中。主要有

raw_contacts 表:用來存放連絡人的 id;

data 表:用來存放連絡人的具體資料;

mimetypes 表:儲存資料類型。

 1 public class ContactInfoProvider { 2  3     private Context context; 4  5     public ContactInfoProvider(Context context) { 6         this.context = context; 7     } 8  9     /**10      * 返回所有的連絡人的資訊11      * 12      * @return13      */14     public List<ContactInfo> getContactInfos() {15     16         // 將所有連絡人存入該集合17         List<ContactInfo> infos = new ArrayList<ContactInfo>();18         // 擷取 raw_contacts 表所對應的 Uri19         Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");20         // 擷取 data 表所對應的 Uri21         Uri datauri = Uri.parse("content://com.android.contacts/data");22         // 參數二:所要查詢的列,即連絡人的id。擷取一個查詢資料庫所返回的結果集23         Cursor cursor = context.getContentResolver().query(uri,    new String[] { "contact_id" }, null, null, null);24         // 移動遊標25         while (cursor.moveToNext()) {26             // 因為我們只需要查詢一列資料-連絡人的id,所以我們傳入027             String id = cursor.getString(0);28             // 用於封裝每個連絡人的具體資訊29             ContactInfo info = new ContactInfo();30             // 得到 id 後,我們通過該 id 來查詢 data 表中的連絡人的具體資料(data表中的data1中的資料)。31             // 參數二:null,會將所有的列返回回來32             // 參數三:選擇條件    返回一個在data表中查詢後的結果集33             Cursor dataCursor = context.getContentResolver().query(datauri,    null, "raw_contact_id=?", new String[] { id }, null);34             while (dataCursor.moveToNext()) {35                 //dataCursor.getString(dataCursor.getColumnIndex("mimetype"))擷取data1列中具體資料的資料類型,這裡判斷的是連絡人的姓名36                 if ("vnd.android.cursor.item/name".equals(dataCursor.getString(dataCursor.getColumnIndex("mimetype")))) {37                     //dataCursor.getString(dataCursor.getColumnIndex("data1"))擷取data1列中的連絡人的具體資料38                     info.setName(dataCursor.getString(dataCursor.getColumnIndex("data1")));39                 } else if ("vnd.android.cursor.item/phone_v2".equals(dataCursor.getString(dataCursor.getColumnIndex("mimetype")))) {//資料類型是否是手機號碼40                     info.setPhone(dataCursor.getString(dataCursor.getColumnIndex("data1")));41                 }42             }43             // 每查詢一個連絡人後就將其添加到集合中44             infos.add(info);45             info = null;46             // 關閉結果集47             dataCursor.close();48         }49         cursor.close();50         return infos;51     }52 }

 

Android 開發工具類 37_ ContactInfoProvider

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.