Some phones need permission READ_CONTACTS to read from contact picker

偶尔善良 提交于 2019-12-22 09:25:05

问题


I have a HTC ONE M7 (GPE 4.2.2) and HTC EVO 3D (4.0.3) HTC Sense 3.6

HTC ONE does not need:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 

on HTC EVO 3D however, following code throws an exception:

 public static String getPhoneNumberFromIntent(Context context, Intent data) throws SecurityException {
    String contactNumber = null;
    final Uri contactUri = data.getData();
    if (contactUri != null) {
        Cursor c = null;
        try {
            // Read contact number from contacts provider
            String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER};
            c = context.getContentResolver().query(contactUri, projection, null, null, null);
            if (c != null && c.moveToFirst()) {
                int maxNumberLength = context.getResources().getInteger(R.integer.max_phone_number_cze);
                contactNumber = cutOnlyLastPhoneNumberDigits(c.getString(0), maxNumberLength);
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }
    return contactNumber;
}

-

java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2
uri content://com.android.contacts/data/2158 from pid=14938, uid=10125 requires android.permission.READ_CONTACTS

I have read that appliaction is granted required permissions when user selects contact by hand. However on some phones this does not work (HTC EVO 3D).

Why is this happending? Is there a workaround such is ability to ask for this permission at runtime ?


回答1:


HTC ONE does not need: <uses-permission android:name="android.permission.READ_CONTACTS" /> on HTC EVO 3D however, following code throws an exception

If the Uri you are getting is coming from ACTION_PICK or ACTION_GET_CONTENT, whether or not you have temporary read permissions for that contact will vary by contact-picking app.

I have read that appliaction is granted required permissions when user selects contact by hand. However on some phones this does not work (HTC EVO 3D).

There is no requirement that all contact-picking apps grant you temporary read access to the contact. In fact, I am unclear if third-party contact managers would have the ability to grant you temporary read access to the contact.

Is there a workaround such is ability to ask for this permission at runtime ?

You cannot change your mix of candidate permissions at runtime.

Your choices are:

  1. Always ask for READ_CONTACTS in the manifest. This ensures you can do what you want, at the cost of requesting another permission, one that prospective users might not like.

  2. Handle the SecurityException and simply do without the data that you are trying to query(), if that data is not essential.

  3. Write a separate app with the READ_CONTACTS permission that can serve as a "plugin" for your app, securely retrieving contact data on behalf of your main app. You can then route users who get the SecurityException to install your contacts plugin. This is tricky to write without introducing security flaws, so I would encourage you to use either of the other options.




回答2:


You can't ask permission at run-time. You can only add a level API to your permission for example:

<uses-permission
     android:name="android.permission.WRITE_EXTERNAL_STORAGE"
     android:maxSdkVersion="18" />

It means that the permission is no needed if api is 19 or higher.



来源:https://stackoverflow.com/questions/23997210/some-phones-need-permission-read-contacts-to-read-from-contact-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!