今天的内容是通过ContentProvider获取联系人。
访问联系人的ContentProvider的URI为
<1>content://com.android.contacts/raw_contacts(获得联系人的id,姓名)
<2>content://com.android.contacts/raw_contacts/"+id+"/data(根据联系人的id获得联系人的信息)
我们先新建Activity与布局文件
布局文件代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.ContactsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/concat"
/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:onClick="getContacts"
android:layout_height="wrap_content"
android:id="@+id/button" />
<Button
android:text="Button2"
android:layout_width="wrap_content"
android:onClick="getPhoto"
android:layout_height="wrap_content"
android:id="@+id/button2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listview"
></ListView>
</LinearLayout>
其中ListView用来展示联系人列表
在Activity的onCreate方法中
Cursor cursor=cr.query(uri,null,null,null,null);
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, new String[]{"_id", "display_name"}, new int[]{R.id.tv_item_list_id, R.id.tv_item_list_name});
listView.setAdapter(simpleCursorAdapter);
while (cursor.moveToNext()){
int id=cursor.getInt(cursor.getColumnIndex("_id"));
String name=cursor.getString(cursor.getColumnIndex("display_name"));
Log.i("test",id+" "+name);
//继续获取相对应的联系人的数据(电话号码)
Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
Cursor cursorData=cr.query(uriData,null,null,null,null);
while(cursorData.moveToNext()){
String data1=cursorData.getString(cursorData.getColumnIndex("data1"));
//int type=cursorData.getInt(cursorData.getColumnIndex("mimetype_id"));
String type=cursorData.getString(cursorData.getColumnIndex("mimetype"));
if("vnd.android.cursor.item/phone_v2".equals(type)){
Log.i("test"," "+data1+":"+type);
}
}
Cursor cursor2 = cr.query(Uri.parse("content://sms/"), new String[]{"body","address"}, "address=?", new String[]{"1 817-396-8101"}, null);
if (cursor2.moveToFirst()) {
do {
for (int i = 0; i < cursor2.getColumnCount(); i++) {
Log.d("customer",""+cursor2.getColumnName(i)+" "+cursor2.getString(i));
}
} while (cursor2.moveToNext());
}
}
获取所有的联系人,效果如下
获取联系人也可以直接跳转至系统的联系人界面直接选择联系人
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,0);
效果如下
点击联系人后返回Activity通过回调方法获取信息
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case 0:
if(data==null)
{
return;
}
//处理返回的data,获取选择的联系人信息
Uri uri=data.getData();
String[] contacts=getPhoneContacts(uri);
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private String[] getPhoneContacts(Uri uri){
String[] contact=new String[2];
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor=cr.query(uri,null,null,null,null);
if(cursor!=null)
{
cursor.moveToFirst();
//取得联系人姓名
int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
contact[0]=cursor.getString(nameFieldColumnIndex);
//取得电话号码
String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
if(phone != null){
phone.moveToFirst();
contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phone.close();
cursor.close();
((TextView) findViewById(R.id.concat)).setText(contact[0]+" "+contact[1]);
}
else
{
return null;
}
return contact;
}
效果如下