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 ?
If the
Uri
you are getting is coming fromACTION_PICK
orACTION_GET_CONTENT
, whether or not you have temporary read permissions for that contact will vary by contact-picking app.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.
You cannot change your mix of candidate permissions at runtime.
Your choices are:
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.Handle the
SecurityException
and simply do without the data that you are trying toquery()
, if that data is not essential.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 theSecurityException
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.You can't ask permission at run-time. You can only add a level API to your permission for example:
It means that the permission is no needed if api is 19 or higher.