Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 722
SoulRayder Avatar asked Nov 27 '16 06:11

SoulRayder


People also ask

What are the basic principles for requesting permissions at runtime?

The basic principles for requesting permissions at runtime are as follows: Ask for permissions in context, when the user starts to interact with the feature that requires it. Don't block the user.

How to request permissions in Android application?

How to Request Permissions in Android Application? Starting from Android 6.0 (API 23), users are not asked for permissions at the time of installation rather developers need to request the permissions at the run time. Only the permissions that are defined in the manifest file can be requested at run time. 1.

What is the function to check and request permission?

// Function to check and request permission. // This function is called when the user accepts or decline the permission. // Request Code is used to check which permission called this function. // This request code is provided when the user is prompt for permission.

What is the use of a permission group?

A permission group merely helps the system minimize the number of system dialogs that are presented to the user when an app requests closely-related permissions. Before you declare and request runtime permissions in your app, evaluate whether your app needs to do so.


Video Answer


1 Answers

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();
    }
}
like image 137
Priyank Patel Avatar answered Nov 03 '22 01:11

Priyank Patel