我正在使用ACTION_PICK意图进行联系,我想从已挑选的联系人中提取姓名和电话。
private static final int CONTANTS_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_urgent_contacts);
Intent contact_picker = new Intent(Intent.ACTION_PICK);
contact_picker.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(contact_picker, CONTANTS_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CONTANTS_REQUEST_CODE && resultCode == RESULT_OK){
onContactPicked(data);
}
}
private void onContactPicked(Intent data) {
Cursor cursor = null;
try {
String contact_phoneNo =null;
String contact_name;
Uri uri = data.getData();
cursor = getContentResolver().query(uri, null, null, null, null);
if(cursor != null && cursor.moveToFirst()){
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
contact_name = cursor.getString(nameIndex);
Integer hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String phone = null;
if (hasPhone > 0) {
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
contact_phoneNo = cursor.getString(phoneIndex);
}
Toast.makeText(this, contact_name+" was added to your urgent contact list", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "phone number: "+contact_phoneNo, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
重新获取联系人名称的工作正常。问题在于电话号码。该应用程序没有崩溃,但我看到Logcat中指向contact_phoneNo = cursor.getString(phoneIndex)
的错误
谢谢您的帮助
被挑选的联系人可能没有电话,或者可能有多个电话,在这种情况下,应用可能会获得用户打算选择的不同电话。
您应该使用电话选择器代替联系人选择器,允许用户选择联系人的特定电话,如下所示:
static final int REQUEST_SELECT_PHONE_NUMBER = 1;
public void selectContactPhone() {
// Start an activity for the user to pick a phone number from contacts
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE); // switches to PHONE PICKER
startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
// Get the URI and query the content provider for the phone number
Uri contactUri = data.getData();
String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, projection,
null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst()) {
int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
...
}
}
}
有关更多信息,请参阅:https://developer.android.com/guide/components/intents-common.html#Contacts
你能为你的要求做些什么,
private void getContactsList() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(String.valueOf(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE));
startActivityForResult(intent, CONTANTS_REQUEST_CODE);
}
现在,要获取所选联系人的详细信息,
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CONTANTS_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
name = cursor.getString(nameColumnIndex);
int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
number = cursor.getString(numberColumnIndex);
if (TextUtils.isEmpty(number)) {
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
Log.d("dataContact", " Name : " + name + ", Number : " + number);
}
}
}
这是您案例的最佳解决方案。