Requesting permissions from user programmatically?
You need to check every time weather permission is granted or not to your app when you want to perform some operation related to that permissions.
Because user may disable that permission from settings any time.
Dangerous permissions and permission groups:
CALENDAR
READ_CALENDAR
WRITE_CALENDAR
CALL_LOG
READ_CALL_LOG
WRITE_CALL_LOG
PROCESS_OUTGOING_CALLS
CAMERA
CAMERA
CONTACTS
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
LOCATION
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
MICROPHONE
RECORD_AUDIO
PHONE
READ_PHONE_STATE
READ_PHONE_NUMBERS
CALL_PHONE
ANSWER_PHONE_CALLS
ADD_VOICEMAIL
USE_SIP
SENSORS
BODY_SENSORS
SMS
SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
STORAGE
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
Source Dangerous permissions
Normal Permissions:
ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
FOREGROUND_SERVICE
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MANAGE_OWN_CALLS
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_COMPANION_RUN_IN_BACKGROUND
REQUEST_COMPANION_USE_DATA_IN_BACKGROUND
REQUEST_DELETE_PACKAGES
REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
SET_ALARM
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS
Source Normal permissions
Get list of required permission programmatically:
public void readPermission()
{
try {
PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_PERMISSIONS);
if (info.requestedPermissions != null) {
for (String p : info.requestedPermissions) {
Log.d(TAG, "Permission : " + p);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
AFAIK, there is no way to read all the permissions from the manifest programmatically. However, since you already have them listed in the Manifest before releasing your APK, you already know which permissions you need to ask for. Don't you?
Let's say you have READ_PHONE_STATE and WRITE_EXTERNAL_STORAGE in your manifest, you can add this in your permission check in your MainActivity.java:
ArrayList<String> arrPerm = new ArrayList<>();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
arrPerm.add(Manifest.permission.READ_PHONE_STATE);
}
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
arrPerm.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if(!arrPerm.isEmpty()) {
String[] permissions = new String[arrPerm.size()];
permissions = arrPerm.toArray(permissions);
ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST);
}
This will trigger permission requests, for both WRITE_EXTERNAL_STORAGE and READ_PHONE_STATE, one after the other. To check which permissions where granted, do this:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0) {
for(int i = 0; i < grantResults.length; i++) {
String permission = permissions[i];
if(Manifest.permission.READ_PHONE_STATE.equals(permission)) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED) {
// you now have permission
}
}
if(Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED) {
// you now have permission
}
}
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
break;
}
}
// other 'case' lines to check for other
// permissions this app might request
}