读取联系人的全系人的权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
首先从三个表中获取到相关的数据,创建一个ArrayList把数据首先放在HashMap中,然后吧Map中的数据放在List中,再去读取list中的数据
raw_contacts表
data表
mimetype表
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
contact_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textColor="#ff0000"/>
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textColor="#000000"/>
</LinearLayout>
MainActrivity.java
package com.ldw.safeContact;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView lvList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvList = (ListView) findViewById(R.id.lv_list);
ArrayList<HashMap<String, String>> readContact = readContact();
System.out.println("HashMap" + readContact);
lvList.setAdapter(new SimpleAdapter(this, readContact,
R.layout.contact_list_item, new String[] {"name", "phone"},
new int[] { R.id.tv_name, R.id.tv_phone }));
}
private ArrayList<HashMap<String, String>> readContact() {
// 首先,从raw_contacts中读取联系人的id(contact_id)
// 然后,根据contact_id从data中查询出相应的电话号码和联系人名称
// 再根据mimeType来区分数据的类型:号码,联系人
Uri rawContactsUri = Uri
.parse("content://com.android.contacts/raw_contacts");
Uri dataUri = Uri.parse("content://com.android.contacts/data");
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
// raw_contacts中查询到contact_id
Cursor rawContactCursor = getContentResolver().query(rawContactsUri,
new String[] { "contact_id" }, null, null, null);
if (rawContactCursor != null) {
while (rawContactCursor.moveToNext()) {
String contactId = rawContactCursor.getString(0);
System.out.println("contactId" + contactId);
// 根据contact_id从data表中查询出相应的电话号码和联系人的名称,实际上查询的是view_data表
Cursor dataCursor = getContentResolver().query(dataUri,
new String[] { "data1", "mimetype" }, "contact_id = ?",
new String[] { contactId }, null);
if (dataCursor != null) {
HashMap<String, String> map = new HashMap<String, String>();
while (dataCursor.moveToNext()) {
String data1 = dataCursor.getString(0);
String mimetype = dataCursor.getString(1);
System.out.println("contactId" + contactId + "data1"
+ data1 + "mimetype" + mimetype);
if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
map.put("phone", data1);
} else if ("vnd.android.cursor.item/name"
.equals(mimetype)) {
map.put("name", data1);
}
}
list.add(map);
dataCursor.close();
}
}
rawContactCursor.close();
}
return list;
}
}
通讯录: