Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 聯絡人操作:查詢、新增與批次交易

編輯:關於 Android 程式設計

Android 聯絡人資料由 ContentProvider 提供,authorities 為 com.android.contacts。

查詢

raw_contacts 保存聯絡人 id,data 保存詳細資料,mimetypes 定義資料類型。可先查 raw_contacts,再以 id 查 data,依 mimetype 分辨姓名、電話、email。

若要依電話查詢,可使用 data/phones/filter/號碼,取得 display_name。

新增

新增聯絡人時,先向 raw_contacts 插入一筆空資料取得 id,再向 data 插入姓名、電話、email 等資料。由於新增需要多次操作,建議使用 applyBatch 搭配 ContentProviderOperation 包成交易,避免資料不完整。

這種批次方式能提升一致性,也較適合一次建立多位聯絡人的情境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved