I have this code to get the following permissions (READ_PHONE_STATE, READ_CONTACTS, WRITE_CONTACTS):
int permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE);
int permissionCheck1 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
int permissionCheck2 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS);
if (permissionCheck != PackageManager.PERMISSION_GRANTED && permissionCheck1 != PackageManager.PERMISSION_GRANTED && permissionCheck2 != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_PHONE_STATE}, 1);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 2);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS}, 3);
} else {
TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
}
I know I don't have these permissions because I get an error like so:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{95e4cb4 30797:com.example.ortel.tagnet/u0a217} (pid=30797, uid=10217) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
Also: It is not requesting my permission to get access to READ_CONTACTS
and WRITE_CONTACTS
.
Whats the issue?
If you have 10 permissions, will you extend your code by 20 more lines. Noooo!
You have 2 ways
Here is my code snippet.
Can it be more simpler?
Now you will put this code Base Activity class and use anywhere you need. Or put in your current class if you don't need it at any other place.
You are using
&&
instead of||
. As it stands, you will not ask for any runtime permissions if you hold just one of the three.Also, you are calling
requestPermissions()
three times. Call it once, with whichever permissions you need.new String[]
creates a string array, and it can hold all three of your desired permission names.