android系统中的联系人是通过ContentProvider向外提供的,以下将结合sqlite数据库中联系人表实现系统联系人的信息查询。联系人数据库的路径为:data/data/com.android.providers.contacts/databases/contacts2.db.系统联系人的操作要关注以下2张表:
1.raw_contacts,这张表中有联系人的contact_id和display_name,根据这两列可以拿到联系人的名字和唯一标识id
2.data,这张表保存了联系人的详细信息。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//首先拿到ContentResolver
ContentResolver resolver = this.getContentResolver();
//根据联系人的Uri拿到联系人的id和name
Uri contactsIdUri = android.provider.ContactsContract.Contacts.CONTENT_URI;
/*查询时选择id列,避免资源的浪费*/
Cursor cursor = resolver.query(contactsIdUri,
new String[]{android.provider.ContactsContract.Contacts._ID,
android.provider.ContactsContract.Contacts.DISPLAY_NAME},
null, null, null);
while(cursor.moveToNext()){
String contactId = cursor.getString(cursor.getColumnIndex(
android.provider.ContactsContract.Contacts._ID));
String contactName = cursor.getString(cursor.getColumnIndex(
android.provider.ContactsContract.Contacts.DISPLAY_NAME));
System.out.println("联系人ID:" + contactId);
System.out.println("联系人名字:" + contactName);
//联系人电话号码的Uri
Uri phoneUri = android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor phones = resolver.query(phoneUri,
null,
android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,
null, null);
while(phones.moveToNext()){
String number = phones.getString(phones.getColumnIndex(
android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("联系人电话号码:" + number);
}
phones.close();
//联系人邮箱的Uri
Uri emailUri = android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_URI;
Cursor emails = resolver.query(emailUri,
null,
android.provider.ContactsContract.CommonDataKinds.Email.CONTACT_ID+"="+contactId,
null, null);
while(emails.moveToNext()){
String email = emails.getString(emails.getColumnIndex(
android.provider.ContactsContract.CommonDataKinds.Email.DATA));
System.out.println("联系人邮箱:" + email);
}
emails.close();
}
cursor.close();
}
}
结果:
存储联系人相关信息的表:
由上面两张表可以看出,联系人的姓名、电话号码、邮箱等信息是mimitype_id是一一对应的,所以我们也可以先拿到联系人的id,然后根据mimetype_id拿到联系人的详细信息。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//首先拿到ContentResolver
ContentResolver resolver = this.getContentResolver();
//根据联系人的Uri拿到联系人的id和name
Uri contactsIdUri = android.provider.ContactsContract.Contacts.CONTENT_URI;
/*查询时选择id列,避免资源的浪费*/
Cursor cursor = resolver.query(contactsIdUri,
new String[]{android.provider.ContactsContract.Contacts._ID},
null, null, null);
while(cursor.moveToNext()){
String contactId = cursor.getString(cursor.getColumnIndex(
android.provider.ContactsContract.Contacts._ID));
System.out.println("联系人ID:" + contactId);
//System.out.println("联系人名字:" + contactName);
//从一个Cursor获取所有的信息
Cursor allInfo = resolver.query(
android.provider.ContactsContract.Data.CONTENT_URI,
new String[]{android.provider.ContactsContract.Data.CONTACT_ID,
android.provider.ContactsContract.Data.MIMETYPE,
android.provider.ContactsContract.Data.DATA1
},
android.provider.ContactsContract.Data.CONTACT_ID+"="+contactId, null, null);
while(allInfo.moveToNext()) {
String mimetype = allInfo.getString(
allInfo.getColumnIndex(android.provider.ContactsContract.Data.MIMETYPE));
String value = allInfo.getString(
allInfo.getColumnIndex(android.provider.ContactsContract.Data.DATA1));
if(mimetype.equals("vnd.android.cursor.item/name")){
System.out.println("联系人姓名:"+value);
} else if(mimetype.contains("/im")){//vnd.android.cursor.item/im
System.out.println("聊天(QQ)账号:"+value);
} else if(mimetype.contains("/email")) {//vnd.android.cursor.item/email_v2
System.out.println("联系人邮箱:"+value);
} else if(mimetype.contains("/phone")) {//vnd.android.cursor.item/phone_v2
System.out.println("联系人电话:"+value);
} else if(mimetype.contains("/postal")) {//vnd.android.cursor.item/postal-address_v2
System.out.println("联系人邮编:"+value);
} else if(mimetype.contains("/photo")) {//vnd.android.cursor.item/photo
System.out.println("联系人照片:"+value);
} else if(mimetype.contains("/group")) {//vnd.android.cursor.item/group_membership
System.out.println("联系人组:"+value);
}
}
System.out.println("****************************");
allInfo.close();
}
cursor.close();
}
}