//启动通讯录页面
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_UPI),0);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
// ContentProvider展示数据类似一个单个数据库表
// ContentResolver实例带的方法可实现找到指定的ContentProvider并获取到ContentProvider的数据
ContentResolver reContentResolverol = getContentResolver();
// URI,每个ContentProvider定义一个唯一的公开的URI,用于指定到它的数据集
Uri contactData = data.getData();
Log.e("tag_uri= ", contactData);
// 查询就是输入URI等参数,其中URI是必须的,其他是可选的,如果系统能找到URI对应的ContentProvider将返回一个Cursor对象.
Cursor cursor = reContentResolverol.query(contactData, null, null, null, null);
cursor.moveToFirst();
// 条件为联系人ID
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
// 获得DATA表中的电话号码,条件为联系人ID,因为手机号码可能会有多个
Cursor phone = reContentResolverol.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null);
while (phone.moveToNext()) {
String usernumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("tag_x", "userName= " + usernumber + "\n name = " + name, getClass());
}
}
}
ContentResolver contentResolver = context.getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
// Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
// cursor.moveToFirst();
while (cursor.moveToNext()){
//拿到联系人id 跟name
// int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phone = cursor.getString(cursor.getColumnIndex("data1"));
LogUtil.e("name = " + name, context.getClass());
LogUtil.e("phone = " + phone, context.getClass());
String[] columnNames = cursor.getColumnNames();
for (String cName:
columnNames) {
LogUtil.e("cName= " + cName, context.getClass());
}
}
"android.permission.READ_CONTACTS" />
http://www.cnblogs.com/zhujiabin/p/6645551.html
public void testGetAllContact() throws Throwable
{
//获取联系人信息的Uri
Uri uri = ContactsContract.Contacts.CONTENT_URI;
//获取ContentResolver
ContentResolver contentResolver = ContactListActivity.this.getContentResolver();
//查询数据,返回Cursor
Cursor cursor = contentResolver.query(uri, null, null, null, null);
List