获取手机通讯录是Android最常用的小功能,今天自学到了,记下来,主要是通过系统自带的内容提供者提供的数据,我们使用内容接收者获取相应的数据到cursor中,然后获取对应data表中的字段,相关字段代表什么含义,只能自己去查了。
下面是手机通讯录列表的代码,仅供参考:
package com.andy.phonecontact; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; /** * @author Zhang,Tianyou * @version 2014年11月18日 下午10:00:46 */ public class MainActivity extends Activity { private ListView select_contact; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); select_contact = (ListView) findViewById(R.id.select_contact); List<Map<String, String>> data = getContactInfo(); select_contact.setAdapter(new SimpleAdapter(MainActivity.this, data, R.layout.contact_item_view, new String[] { "name", "phone" }, new int[] { R.id.tv_name, R.id.tv_phone })); } /** * 读取手机里的联系人信息 * * @return */ private List<Map<String, String>> getContactInfo() { // 把所有的联系人放到list List<Map<String, String>> list = new ArrayList<Map<String, String>>(); // 得到一个内容解析器 ContentResolver resolver = getContentResolver(); // 获取联系人表对应的内容提供者url raw_contacts表和data表 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri datauri = Uri.parse("content://com.android.contacts/data"); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); // 获取contact_id 获取联系人id while (cursor.moveToNext()) { String contact_id = cursor.getString(0); if (contact_id != null) { // 具体的某个联系人 Map<String, String> map = new HashMap<String, String>(); // 如果不为空 查询对应data表的联系人信息 Cursor dataCursor = resolver.query(datauri, new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contact_id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); System.out.println("data1 ==" + data1 + "mimetype == " + mimetype); if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) { System.out.println("电话:" + data1); map.put("phone", data1); } else if ("vnd.android.cursor.item/name".equals(mimetype)) { System.out.println("姓名:" + data1); map.put("name", data1); } } list.add(map); // 释放游标 dataCursor.close(); } } cursor.close(); return list; } }
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.andy.phonecontact.MainActivity" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/select_contact" /> </RelativeLayout>
<?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:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" android:textColor="#ff0000" android:textSize="22sp" /> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@android:drawable/ic_menu_call" android:text="5558" android:textColor="#000000" android:textSize="22sp" /> </LinearLayout>
<uses-permission android:name="android.permission.READ_CONTACTS"/>