Reputation: 621
In my app I want give user possibility to fill text form with phone number by manually typing it or selecting phone number from contacts list. One thing I don't understand is why I should set READ_CONTACTS
permission if user select contact by himself.I am using code listed below:
To start Contacts activity:
Intent pickContactIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST_CODE);
To process Intent
data from onActivityResult
:
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver()
.query(
uri,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE }, null, null,
null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
int type = c.getInt(1);
showSelectedNumber(type, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
And as I understand getContentResolver().query()
requires READ_CONTACTS
permission to obtain phone number.
My question: is it possible somehow to process Intent that come in onActivityResult without READ_CONTACTS
?
Upvotes: 0
Views: 2617
Reputation: 3418
with or without placing the permission in your manifest, You will need to request that permission from the User on-the-fly.
if( getApplicationContext().checkSelfPermission( Manifest.permission.READ_CONTACTS ) != PackageManager.PERMISSION_GRANTED )
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CONTACTS}, resultValue);
only then after the approval you will be able to query the contacts phone number/name etc.
Upvotes: -1
Reputation: 1982
you SHOULDN'T need READ_CONTACTS actually. @Tony Chu is right, the documentation says:
Note: Before Android 2.3 (API level 9), performing a query on the Contacts Provider (like the one shown above) requires that your app declare the READ_CONTACTS permission (see Security and Permissions). However, beginning with Android 2.3, the Contacts/People app grants your app a temporary permission to read from the Contacts Provider when it returns you a result. The temporary permission applies only to the specific contact requested, so you cannot query a contact other than the one specified by the intent's Uri, unless you do declare the READ_CONTACTS permission. (Source)
HOWEVER, I've noticed that some phones may require it nonetheless (xperia devices in my experience and also HTC ones according to @AVirvara).
Sorry but it seems like you won't have any alternative.
Upvotes: 1
Reputation: 1048
You don't need READ_CONTACTS actually.
Note: Before Android 2.3 (API level 9), performing a query on the Contacts Provider (like the one shown above) requires that your app declare the READ_CONTACTS permission (see Security and Permissions). However, beginning with Android 2.3, the Contacts/People app grants your app a temporary permission to read from the Contacts Provider when it returns you a result. The temporary permission applies only to the specific contact requested, so you cannot query a contact other than the one specified by the intent's Uri, unless you do declare the READ_CONTACTS permission.
Source: http://developer.android.com/training/basics/intents/result.html
Upvotes: 3
Reputation: 2379
Yous should write permission in manifest file then u only able to read contact from file.
Upvotes: 0
Reputation: 21191
You can not access Android contacts dababase without giving READ_CONTACTS
permission in your manifest file.
The Android mobile OS uses a permission system to help ensure that apps behave appropriately. The system allows apps to request access to features on the device that can use power, access sensitive data, and incur charges.
Upvotes: 0