protected final int REQUEST_PERMISSION_ADDRESSBOOK = 10;
protected final int REQUEST_ADDRESSBOOK = 11;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_PERMISSION_ADDRESSBOOK);
}else{
// 跳转到联系人界面
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("vnd.android.cursor.dir/phone_v2");
startActivityForResult(intent, REQUEST_ADDRESSBOOK);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_ADDRESSBOOK){
if (grantResults.length > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
// 跳转到联系人界面
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("vnd.android.cursor.dir/phone_v2");
startActivityForResult(intent, REQUEST_ADDRESSBOOK);
}else {
new AlertDialog.Builder(BaseApplication.get().getTopActivity())
.setTitle(R.string.warm_tips)
.setMessage("您已经拒绝了获取联系人权限,请手动设置")
.setPositiveButton("去设置", (dialogInterface, i) -> SysIntentUtil.goSettings(BaseApplication.get()))
.setNegativeButton("不了", null)
.show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//选择联系人的回调
if (requestCode == REQUEST_ADDRESSBOOK) {
try {
if (data != null) {
Uri uri = data.getData();
String phoneNum = null;
String contactName = null;
// 创建内容解析者
ContentResolver contentResolver = getContentResolver();
Cursor cursor = null;
if (uri != null) {
cursor = contentResolver.query(uri, new String[]{"display_name", "data1"}, null, null, null);
}
while (cursor.moveToNext()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursor.close();
if (phoneNum != null) {
phoneNum = phoneNum.replaceAll("-", " ");
phoneNum = phoneNum.replaceAll(" ", "");
}
Toast.makeText(this,contactName+ "=="+phoneNum,Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this,"用户未选择联系人",Toast.LENGTH_LONG).show(); //用户未选择联系人
}
}catch (Exception e){Toast.makeText(this,"获取联系人失败",Toast.LENGTH_LONG).show();} //异常
}
}
好了,大概就这4步就可以获取到联系人的姓名和手机号码了;
直接上代码啦!!!
public abstract class BaseActivity extends AppCompatActivity {
/***************************你基类中其它代码*********************/
protected final int REQUEST_PERMISSION_ADDRESSBOOK = 10;
protected final int REQUEST_ADDRESSBOOK = 11;
interface OnNativeAddressBookListener{
void onNativeAddress(String assressBook);
}
private OnNativeAddressBookListener onNativeAddressBookListener;
protected void getNativeAddressBook(OnNativeAddressBookListener onNativeAddressBookListener){
this.onNativeAddressBookListener = onNativeAddressBookListener;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_PERMISSION_ADDRESSBOOK);
}else{
// 跳转到联系人界面
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("vnd.android.cursor.dir/phone_v2");
startActivityForResult(intent, REQUEST_ADDRESSBOOK);
}
}
private void callbackNativeAddressBook(int code , String name , String phone){
Map addressBookMap = new HashMap<>();
addressBookMap.put("name",name);
addressBookMap.put("phone",phone);
addressBookMap.put("errorCode",code);
if (code != 0) {addressBookMap.put("errorMessage","获取联系人信息失败,请选择联系人或检查通讯录权限是否开启");}
if (onNativeAddressBookListener != null) onNativeAddressBookListener.onNativeAddress(GsonUtils.toJson(addressBookMap));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//选择联系人的回调
if (requestCode == REQUEST_ADDRESSBOOK) {
try {
if (data != null) {
Uri uri = data.getData();
String phoneNum = null;
String contactName = null;
// 创建内容解析者
ContentResolver contentResolver = getContentResolver();
Cursor cursor = null;
if (uri != null) {
cursor = contentResolver.query(uri, new String[]{"display_name", "data1"}, null, null, null);
}
while (cursor.moveToNext()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursor.close();
if (phoneNum != null) {
phoneNum = phoneNum.replaceAll("-", " ");
phoneNum = phoneNum.replaceAll(" ", "");
}
callbackNativeAddressBook(0, contactName, phoneNum);
} else {
callbackNativeAddressBook(-2, null, null); //用户未选择联系人
}
}catch (Exception e){callbackNativeAddressBook(-3, null, null);} //异常
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_ADDRESSBOOK){
if (grantResults.length > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
// 跳转到联系人界面
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("vnd.android.cursor.dir/phone_v2");
startActivityForResult(intent, REQUEST_ADDRESSBOOK);
}else {
callbackNativeAddressBook(-1, null, null);
new AlertDialog.Builder(BaseApplication.get().getTopActivity())
.setTitle(R.string.warm_tips)
.setMessage("您已经拒绝了获取联系人权限,请手动设置")
.setPositiveButton("去设置", (dialogInterface, i) -> SysIntentUtil.goSettings(BaseApplication.get()))
.setNegativeButton("不了", null)
.show();
}
}
}
}
使用:继承BaseActivity调用这个方法就行了,就和设置个Button的点击事件一样简单;
getNativeAddressBook(new OnNativeAddressBookListener() {
@Override
public void onNativeAddress(String assressBook) {
Log.i(callback, assressBook);
}
});
注:一些工具类就不贴出来了
Gson:GsonUtils.toJson(addressBookMap); 把对象转Json
SysIntentUtil.goSettings(BaseApplication.get()); 打开应用的设置界面;