Search code examples
androidandroid-manifestandroid-permissions

Requesting permissions from user programmatically?


I want to add the feature of giving the user the list of permissions to approve just after opening the app for the first time.

I read from this article regarding how to do this.

But I still have some queries regarding this, to make this feature of my app truly dynamic:

  • How to read from manifest file the list of required permission? (This to avoid explicit hard-coded verification of each required permissions)

  • How to programatically classify these permissions as NORMAL and DANGEROUS, since I need to explicitly ask the user only when the permission is of dangerous kind? (As per documentation, normal permissions are automatically granted by the OS without requesting the user)


Solution

  • 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();
        }
    }