3

我想支持使用联系人选择器添加联系人。似乎某些手机需要 READ_CONTACTS 权限而不是其他手机 - 请参阅联系人选择器查询是否需要 read_contacts 权限,具体取决于 android 版本?

    Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
    startActivityForResult(intent, CHOOSE_CONTACT);

当我查询返回的数据(mangedQUery())时,onActivityResult()发生异常

     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 始终为空,因此无法正常工作:

    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;
4

0 回答 0