首先在布局文件中放置一个listview,然后找到ID
系统联系人提供了一个内容提供者,通过内容解析器,匹配Url地址
1,内容解析器
2,Url地址,查看系统联系人数据库,内容提供者源码
先看清单文件
后看java类(联系人数据库有多张表)
contents://com.android.contacts/表名
3,系统联系人数据库中核心表,表结构
raw_contacts 联系人表: contact_id 联系人唯一性id值
data 用户信息表:raw_contact_id作为外键,和raw_contacts中contact_id做关联查询
获取data1字段,包含了电话号码以及联系人名称
mimetype_id字段,包含了当前行data1对应的数据类型
mimetypes 类型表: 获取data表中mimetype_id和mimetypes中_id做关联查询,获取指向的信息类型
电话号码:vnd.android.cursor.item/phone_v2
用户名称:vnd.android.cursor.item/name
google,data表和mimetypes生成了一个视图
4,表的访问方式
content://com.android.contacts/raw_contacts
content://com.android.contacts/data
public class ContactListActivity extends Activity {
private static final String TAG = "ContactListActivity";
private ListView lv_contact;
private List<HashMap<String, String>> mContactlist = new ArrayList<HashMap<String, String>>();
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//当list集合数据准备完毕,把适配器设置到listview上
mAdapter = new MyAdapter();
lv_contact.setAdapter(mAdapter);
}
};
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
initUi();
initDate();
}
private void initUi() {
lv_contact = (ListView) findViewById(R.id.lv_contact);
lv_contact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mAdapter != null) {
//返回电话号码
HashMap<String, String> hashMap = mAdapter.getItem(position);
String phone = hashMap.get("phone");
Intent intent = new Intent();
intent.putExtra("phone", phone);
setResult(0, intent);
finish();
}
}
});
}
private void initDate() {
//开启子线程
new Thread(new Runnable() {
@Override
public void run() {
ContentResolver contentResolver = getContentResolver();
//查询raw_contacts的contact_id列
Cursor cursor = contentResolver.query(Uri.parse("content://com.android.contacts/raw_contacts"),
new String[]{"contact_id"}, null, null, null);
//存储之前最好清理一下,以免数据混乱
mContactlist.clear();
if (cursor != null) {
while (cursor.moveToNext()) {
String id = cursor.getString(0);
Log.d(TAG, "ID:" + id);
//根据之前查询的ID值(raw_contact_id列)查询对应data1列和mimetype列
Cursor cursor1 = contentResolver.query(Uri.parse("content://com.android.contacts/data"),
new String[]{"data1", "mimetype"}, "raw_contact_id = ?", new String[]{id}, null);
HashMap<String, String> contactmap = new HashMap<String, String>();
if (cursor1 != null) {
while (cursor1.moveToNext()) {
String data = cursor1.getString(0);
String type = cursor1.getString(1);
if (type.equals("vnd.android.cursor.item/phone_v2")) {
if (!TextUtils.isEmpty(data)) {
contactmap.put("phone", data);
Log.d(TAG, "phone: " + data);
}
} else if (type.equals("vnd.android.cursor.item/name")) {
if (!TextUtils.isEmpty(data)) {
contactmap.put("name", data);
Log.d(TAG, "name: " + data);
}
}
}
Log.d(TAG, "MAP: " + contactmap);
}
//一定要记得关闭结果集
if (!contactmap.isEmpty()) {
mContactlist.add(contactmap);
Log.d(TAG, "LIST: " + mContactlist);
}
cursor1.close();
}
}
cursor.close();
//集合数据准备完毕,发送消息给主线程跟新UI
mHandler.sendEmptyMessage(0);
}
}).start();
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return mContactlist.size();
}
@Override
public HashMap<String, String> getItem(int position) {
return mContactlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView != null) {
view = convertView;
} else {
view = View.inflate(getApplication(), R.layout.contact_list_item, null);
}
TextView tv_item_name = (TextView) view.findViewById(R.id.tv_item_name);
TextView tv_item_phone = (TextView) view.findViewById(R.id.tv_item_phone);
HashMap<String, String> hashMap = getItem(position);
tv_item_name.setText(hashMap.get("name"));
tv_item_phone.setText(hashMap.get("phone"));
return view;
}
}
}