android 怎么获取手机联系人csdn
1个回答
2016-09-17
展开全部
可以参考如下信息,读取手机联系人,不过事先需要权限:
/*
* 读取联系人的信息
*/
public void testReadAllContacts() {
Cursor cursor = this.getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
int contactIdIndex = 0;
int nameIndex = 0;
if(cursor.getCount() > 0) {
contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
}
while(cursor.moveToNext()) {
String contactId = cursor.getString(contactIdIndex);
String name = cursor.getString(nameIndex);
Log.i(TAG, contactId);
Log.i(TAG, name);
/*
* 查找该联系人的phone信息
*/
Cursor phones = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
null, null);
int phoneIndex = 0;
if(phones.getCount() > 0) {
phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
}
while(phones.moveToNext()) {
String phoneNumber = phones.getString(phoneIndex);
Log.i(TAG, phoneNumber);
}
/*
* 查找该联系人的email信息
*/
Cursor emails = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId,
null, null);
int emailIndex = 0;
if(emails.getCount() > 0) {
emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
}
while(emails.moveToNext()) {
String email = emails.getString(emailIndex);
Log.i(TAG, email);
}
}
}
/*
* 读取联系人的信息
*/
public void testReadAllContacts() {
Cursor cursor = this.getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
int contactIdIndex = 0;
int nameIndex = 0;
if(cursor.getCount() > 0) {
contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
}
while(cursor.moveToNext()) {
String contactId = cursor.getString(contactIdIndex);
String name = cursor.getString(nameIndex);
Log.i(TAG, contactId);
Log.i(TAG, name);
/*
* 查找该联系人的phone信息
*/
Cursor phones = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
null, null);
int phoneIndex = 0;
if(phones.getCount() > 0) {
phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
}
while(phones.moveToNext()) {
String phoneNumber = phones.getString(phoneIndex);
Log.i(TAG, phoneNumber);
}
/*
* 查找该联系人的email信息
*/
Cursor emails = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId,
null, null);
int emailIndex = 0;
if(emails.getCount() > 0) {
emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
}
while(emails.moveToNext()) {
String email = emails.getString(emailIndex);
Log.i(TAG, email);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |