android 跳转到系统通讯录


跳转到系统的通信录列表,并读取联系人电话

需要权限

<uses-permission android:name="android.permission.READ_CONTACTS"/>

6.0之后需要动态申请权限

int readStoragePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
if (readStoragePermission != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACT);
} else {
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, GET_CONTACT);
}
 
  
//请求权限回调
 
  
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_READ_CONTACT://读取手机联系人权限
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
		Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    		startActivityForResult(intent, GET_CONTACT);
            } else {
                // Permission Denied
            }
            break;
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
 
  
//获取点击选中的联系人回调
 
  
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case GET_CONTACT: 
 
  
	if (resultCode == RESULT_OK) {
 
  
	     Uri uri = data.getData();
	     getPhone(uri);
	}
         break;			
	   
}
 
  
 
  
private void getPhone(Uri uri) {
        if (uri != null) {
            Cursor c = getContentResolver().query(uri, null, null, null, null);
            if (c == null) {
                return;
            }
            if (c.moveToFirst()) {
                String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                if ("1".equalsIgnoreCase(hasPhone)) {//判断是否含有手机号
                    Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                                    + contactId,
                            null,
                            null);
                    while (phoneCursor.moveToNext()) {//循环该联系人下所有电话号码
                        String phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//                        String name = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    }
                    phoneCursor.close();
                } else {
                    Toast.makeText(this,"联系人手机号码不能为空",Toast.LENGTH_SHORT).show();
                }
                c.close();
            }
        }
    }

你可能感兴趣的:(android知识库)