編輯:關於 Android 程式設計
想在自己的應用裡列出通訊錄,本質上是透過 ContentResolver 查詢系統的 Contacts 內容提供者。整個流程分兩步:先定義列表項的樣式,再撰寫查詢邏輯。
新增 contact_item.xml,左側放姓名,右側放電話號碼並搭配一個撥號圖示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="姓名:"
android:textColor="#000000"
android:textSize="20dp" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="80dp"
android:layout_marginTop="10dip"
android:drawableLeft="@android:drawable/ic_menu_call"
android:text="電話號"
android:textColor="#000000"
android:textSize="20dp" />
</RelativeLayout>
關鍵觀念是:聯絡人資料被拆成兩層。raw_contacts 提供 contact_id 清單,而真正的姓名、電話等欄位則存在 data 表中,需要再用 contact_id 二次查詢,並依 mimetype 判斷這筆資料究竟是哪一類。
private List<Map<String, String>> getContactInfo() {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri uriData = Uri.parse("content://com.android.contacts/data");
Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(0);
if (contactId == null) {
continue;
}
Map<String, String> map = new HashMap<String, String>();
Cursor dataCursor = resolver.query(uriData,
new String[] { "data1", "mimetype" },
"contact_id=?", new String[] { contactId }, null);
while (dataCursor.moveToNext()) {
String data1 = dataCursor.getString(0);
String mimetype = dataCursor.getString(1);
if ("vnd.android.cursor.item/name".equals(mimetype)) {
map.put("name", data1);
} else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
map.put("phone", data1);
}
}
list.add(map);
dataCursor.close();
}
cursor.close();
return list;
}
list_select_contact = (ListView) findViewById(R.id.list_select_contact);
List<Map<String, String>> data = getContactInfo();
list_select_contact.setAdapter(new SimpleAdapter(this, data,
R.layout.contact_item,
new String[] { "name", "phone" },
new int[] { R.id.tv_name, R.id.tv_phone }));
權限不能少。需在 AndroidManifest.xml 中聲明 android.permission.READ_CONTACTS;Android 6.0 以上還必須在執行期動態申請,否則 query 會直接丟出 SecurityException。
API 已過時。這裡使用的 content://com.android.contacts/ 在 API 5 之後已被 ContactsContract 取代。新專案建議改用 ContactsContract.Contacts 搭配 ContactsContract.CommonDataKinds.Phone,並以 Data.CONTACT_ID 做關聯查詢,能少掉一層巢狀迴圈。
另外別忘了:Cursor 一定要關閉,否則在大量資料下很容易觸發 cursor leak 警告。
OpenCV 移植到 Android 平台的環境配置
OpenCV 是常見的電腦視覺函式庫,而 Android 開發主要使用 Java。若要在 Android 中使用 OpenCV,通常需要透過 NDK 與 JNI 銜接。本
Android SDK 與 Eclipse ADT 離線安裝教程
由於某些原因,無論是否翻牆,從官方下載 Android SDK 的速度都慢得讓人崩潰。所以這裡分享一下離線安裝 Android SDK 和 Eclipse ADT 的方法
四大組件之 Activity(上):生命週期、系統回收與配置改變
第一節 Activity 的使用 Activity 幾乎是每個應用必備的組件,因此 Android 開發幾乎都從 Activity 開始。舉例來說,若要設計一個計算器應用
LRecyclerView 進階用法:側滑菜單、長按拖拽、滑動刪除一次搞定
LRecyclerView 現在能做什麼 反覆思考並兼顧使用便利性之後,LRecyclerView 整合了 SwipeMenu 系列能力:Item 左右側滑菜單、長按拖拽