代替使用电话选择器,而使用联系人选择器,然后显示电话对话框。
Intent intent = Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_SELECT_CONTACT);
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
long contactId = getContactIdFromUri(contactUri);
List<String> phones = getPhonesFromContactId(contactId);
showPhonesDialog(phones);
}
}
private long getContactIdFromUri(Uri contactUri) {
Cursor cur = getContentResolver().query(contactUri, new String[]{ContactsContract.Contacts._ID}, null, null, null);
long id = -1;
if (cur.moveToFirst()) {
id = cur.getLong(0);
}
cur.close();
return id;
}
private List<String> getPhonesFromContactId(long contactId) {
Cursor cur = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI,
new String[]{CommonDataKinds.Phone.NUMBER},
CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{String.valueOf(contactId)}, null);
List<String> phones = new ArrayList<>();
while (cur.moveToNext()) {
String phone = cur.getString(0);
phones.add(phone);
}
cur.close();
return phones;
}
private void showPhonesDialog(List<String> phones) {
String[] phonesArr = phones.toArray(new String[0]);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select a phone:");
builder.setItems(phonesArr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Phone Selection", "user selected: " + phonesArr[which]);
}
});
builder.show();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句