- 1,添加权限
<!-- 获取通讯录权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
- 2,代码,跳转
Intent intent3 = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent3, REQUEST_ADDPHONE);
- 3,获取得到的结果
switch (requestCode) {
case REQUEST_ADDPHONE:
if (resultCode == RESULT_OK) {
try {
//处理返回的data,获取选择的联系人信息
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String[] str_data = PhoneUtils.getContactPhone(cursor,this);
Log.d("date",str_data[0] + str_data[1]);
Toast.makeText(this, str_data[0] + str_data[1] ,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "获取联系人失败", Toast.LENGTH_SHORT).show();
}
}
break;
}
- 4,工具类
public class PhoneUtils {
public static String[] getContactPhone(Cursor cursor, Context context) {
int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int phoneNum = cursor.getInt(phoneColumn);
String[] str = new String[2];
String username = "";
String result = "";
if (phoneNum > 0) {
// 获得联系人的ID号
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
// 获得联系人电话的cursor
Cursor phone = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
if (phone.moveToFirst()) {
for (; !phone.isAfterLast(); phone.moveToNext()) {
int index = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phoneNumber = phone.getString(index);
str[0] = phoneNumber;
username = getContactNameFromPhoneBook(context, phoneNumber);
str[1] = username;
}
if (!phone.isClosed()) {
phone.close();
}
}
}
return str;
}
public static String getContactNameFromPhoneBook(Context context, String phoneNum) {
String contactName = "";
ContentResolver cr = context.getContentResolver();
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
new String[]{phoneNum}, null);
if (pCur.moveToFirst()) {
contactName = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
pCur.close();
}
return contactName;
}
}
<!-- 获取通讯录权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
- 2,代码,跳转
Intent intent3 = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent3, REQUEST_ADDPHONE);
- 3,获取得到的结果
switch (requestCode) {
case REQUEST_ADDPHONE:
if (resultCode == RESULT_OK) {
try {
//处理返回的data,获取选择的联系人信息
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String[] str_data = PhoneUtils.getContactPhone(cursor,this);
Log.d("date",str_data[0] + str_data[1]);
Toast.makeText(this, str_data[0] + str_data[1] ,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "获取联系人失败", Toast.LENGTH_SHORT).show();
}
}
break;
}
- 4,工具类
public class PhoneUtils {
public static String[] getContactPhone(Cursor cursor, Context context) {
int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int phoneNum = cursor.getInt(phoneColumn);
String[] str = new String[2];
String username = "";
String result = "";
if (phoneNum > 0) {
// 获得联系人的ID号
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
// 获得联系人电话的cursor
Cursor phone = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
if (phone.moveToFirst()) {
for (; !phone.isAfterLast(); phone.moveToNext()) {
int index = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phoneNumber = phone.getString(index);
str[0] = phoneNumber;
username = getContactNameFromPhoneBook(context, phoneNumber);
str[1] = username;
}
if (!phone.isClosed()) {
phone.close();
}
}
}
return str;
}
public static String getContactNameFromPhoneBook(Context context, String phoneNum) {
String contactName = "";
ContentResolver cr = context.getContentResolver();
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
new String[]{phoneNum}, null);
if (pCur.moveToFirst()) {
contactName = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
pCur.close();
}
return contactName;
}
}