3

I want to support adding Contacts with the Contact Picker. It appears that the READ_CONTACTS permission is required in certain phones and not others - see 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);

The exception occurs in onActivityResult() when I query the returned data (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();
                                }
                       }
              }
     }

I want to know how I can check beforehand if startActivity will fail, so I can disable the functionality before having to do this "live check".

Tried this as a possible solution, but did not work as ai.permission is always 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;
Community
  • 1
  • 1
amit
  • 1,373
  • 2
  • 16
  • 29
  • Unfortunately, I do not think you can determine that. `PackageManager` will still report the activity from methods like `resolveActivity()` -- `PackageManager` ignores security issues. Moreover, the other question is incorrect AFAIK. – CommonsWare Jul 22 '12 at 18:34
  • Preliminary solution is to check if version is below 2.3.3, and disable if that is the case. – amit Jul 23 '12 at 05:42

0 Answers0