我想支持使用联系人选择器添加联系人。某些手机似乎需要READ_CONTACTS权限,而其他手机则不需要 - 请参阅Do contact picker queries require the read_contacts permission depending on the android version?
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, CHOOSE_CONTACT);
当我查询返回的数据时,onActivityResult()发生异常(mangedQUery())
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
if (contactData != null) {
Cursor mCursor = managedQuery(contactData, null, null, null, null);
if (mCursor != null && mCursor.moveToFirst()) {
long id = mCursor.getLong(mCursor.getColumnIndex(People._ID));
String name = mCursor.getString(mCursor.getColumnIndex(People.DISPLAY_NAME));
if (name != null) {
displayName(name);
_contact_id = id;
_contact_name = name.trim();
}
}
}
}
我想知道如何在事先检查startActivity是否会失败,所以我可以在执行“实时检查”之前禁用该功能。
尝试将此作为一种可能的解决方案,但由于ai.permission始终为null,因此无效:
final PackageManager packageManager = getPackageManager();
final Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolve : list) {
ActivityInfo ai = resolve.activityInfo;
if (ai.permission != null) {
return false;
}
}
return true;