我正在通过我的应用创建联系人。但是,如果任何联系人应用程序不在手机中,那么它给予ANR。如何检查手机中是否安装了任何联系人应用程序。
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, contactNumber)
.putExtra(ContactsContract.Intents.Insert.NAME, contactName);
答案 0 :(得分:0)
当没有安装app来调用你的意图时调用catch块,你可以在块上通知用户
try {
startActivity(intent);
} catch (ActivityNotFoundException ex) {
//Do something
}
答案 1 :(得分:0)
您可以使用ContentProvider创建联系人,而不是使用Intent
。这不需要在您的设备上安装联系人的应用程序。您可以按照此SO进行操作。
答案 2 :(得分:0)
示例:
public void contactIntent(View v){
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForReslut
switch (requestCode) {
case RESULT_PICK_CONTACT:
contactPicked(data);
break;
}
} else {
Log.e("MainActivity", "Failed to pick contact");
}
}
/**
* Query the Uri and read contact details. Handle the picked contact data.
* @param data
*/
private void contactPicked(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null ;
String name = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
// Set the value to the textviews
textView1.setText(name);
textView2.setText(phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
}