参考:http://www.2cto.com/kf/201409/336307.html
第一步:我们可以在File Explorer视图下找到contacts2.db文件,这是通讯录的文件
将其导入SQLite中查看raw-contacts表
data表
mimetype表
根据raw-contacts表中的_id键与data表中的raw_contact_id键进行数据的定位
直接贴代码
package com.zaizai.safty;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 获取联系人
* Created by zaizai on 2015/10/29.
*/
public class SelectContactActivity extends Activity {
private static final String TAG = "zaizai";
private ListView listSelectContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_contact);
listSelectContact = (ListView) this.findViewById(R.id.list_select_contact);
/*注意数据在内部类中使用,需设置为final类型,因为如果当在加载过程中如果data里的数据变化了,会产生数据异常*/
final List<Map<String, String>> data = getContactInfo();
listSelectContact.setAdapter(new SimpleAdapter(this, data, R.layout.contact_item_view, new String[]{"name", "phone"}, new int[]{R.id.tv_name, R.id.tv_phone}));
listSelectContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String phone = data.get(position).get(BaseSetupActivity.PHONE);
/*将号码以Intent的形式返回*/
Intent data = new Intent();
data.putExtra(BaseSetupActivity.PHONE, phone);
/*RESULT_OK为返回的状态吗,接收方通过状态码进行数据的判断*/
setResult(Activity.RESULT_OK, data);
/*关闭当前页面*/
SelectContactActivity.this.finish();
}
});
}
/**
* 读取手机里面的联系人
*
* @return
*/
private List<Map<String, String>> getContactInfo() {
/*用来临时存储联系人*/
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
/*1、得到内容解析器*/
ContentResolver resolver = this.getContentResolver();
/*2、查询数据库*/
//打得到raw_contacts表,
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
/*获得data表*/
Uri uriData = Uri.parse("content://com.android.contacts/data");
Cursor cursor = resolver.query(uri, new String[]{"contact_id"}, null, null, null);
while (cursor.moveToNext()) {
/*得到联系人ID*/
String contact_id = cursor.getString(0);
if (contact_id != null && contact_id.length() > 0) {
/*得到一个联系人*/
Map<String, String> map = new HashMap<String, String>();
/*查询data表中的data1和mimetype两列的数据*/
Cursor cursorData = resolver.query(uriData, new String[]{"data1", "mimetype"}, "contact_id=?", new String[]{contact_id}, null);
while (cursorData.moveToNext()) {
String data1 = cursorData.getString(0);
String mimetype = cursorData.getString(1);
Log.i(TAG, "data1=" + data1 + "mimetype = " + mimetype);
if ("vnd.android.cursor.item/name".equals(mimetype)) {
/*联系人姓名*/
map.put(BaseSetupActivity.NAME, data1);
}
if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
/*联系人电话号码*/
map.put(BaseSetupActivity.PHONE, data1);
}
}
list.add(map);
cursorData.close();
}
}
cursor.close();
return list;
}
}
布局文件
<?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="@android:color/holo_red_dark"
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="558" />
</LinearLayout>