Android Manual Check and Apply Permission Method

  • 2021-09-24 23:41:46
  • OfStack

Android Permission 1 is generally declared in AndroidManifest. xml, and the user will be automatically prompted whether to provide permissions when installed or used for the first time

Android official document:


Then, when the application is installed on the device, the installer determines whether or not to grant the requested permission by checking the authorities that signed the application's certificates and, in some cases, asking the user. If the permission is granted, the application is able to use the protected features. If not, its attempts to access those features will simply fail without any notification to the user.

However, sometimes, due to mobile phone devices and other reasons, a prompt box does not pop up at the beginning of 1 to let users confirm whether to provide permissions. For some permissions, if they are not confirmed, they will not provide permissions, so some functions cannot be realized.

At this time, we need to manually check whether we already have permission, and if not, we will call the code to prompt the user to provide permission.

How to Manually Check and Request Permissions

For example, check whether you have WRITE_EXTERNAL_STORAGE permission before writing, and apply for permission if you don't

Code:


if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
  != PackageManager.PERMISSION_GRANTED) {
 // Application WRITE_EXTERNAL_STORAGE Authority 
 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
   WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
}

After requesting permission, the system will pop up Dialog for requesting permission. You can allow it

When the user selects Allow or Deny, the onRequestPermissionsResult method is called back, which is similar to onActivityResult


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 doNext(requestCode,grantResults);
}

PS:

In Android 6.0, some permissions only need to be declared in the AndroidManifest. xml file, but others need to be judged at run time in addition to declaration, that is, user confirmation. The permissions that need to be confirmed are as follows:


 Body sensor  
 Calendar  
 Camera  
 Address book  
 Geographical location  
 Microphone  
 Telephone  
 SMS  
 Storage space 

Android6.0 system defaults to applications with targetSdkVersion less than 23 and grants all the applied permissions by default.

So if your previous APP set targetSdkVersion below 23, it won't crash at runtime,

However, this is only a temporary emergency policy, and users can still cancel the granted permissions in the settings.

The following permissions only need to be declared in AndroidManifest. xml to use:


android.permission.ACCESS_LOCATION_EXTRA_COMMANDS 
android.permission.ACCESS_NETWORK_STATE 
android.permission.ACCESS_NOTIFICATION_POLICY 
android.permission.ACCESS_WIFI_STATE 
android.permission.ACCESS_WIMAX_STATE 
android.permission.BLUETOOTH 
android.permission.BLUETOOTH_ADMIN 
android.permission.BROADCAST_STICKY 
android.permission.CHANGE_NETWORK_STATE 
android.permission.CHANGE_WIFI_MULTICAST_STATE 
android.permission.CHANGE_WIFI_STATE 
android.permission.CHANGE_WIMAX_STATE 
android.permission.DISABLE_KEYGUARD 
android.permission.EXPAND_STATUS_BAR 
android.permission.FLASHLIGHT 
android.permission.GET_ACCOUNTS 
android.permission.GET_PACKAGE_SIZE 
android.permission.INTERNET 
android.permission.KILL_BACKGROUND_PROCESSES 
android.permission.MODIFY_AUDIO_SETTINGS 
android.permission.NFC 
android.permission.READ_SYNC_SETTINGS 
android.permission.READ_SYNC_STATS 
android.permission.RECEIVE_BOOT_COMPLETED 
android.permission.REORDER_TASKS 
android.permission.REQUEST_INSTALL_PACKAGES 
android.permission.SET_TIME_ZONE 
android.permission.SET_WALLPAPER 
android.permission.SET_WALLPAPER_HINTS 
android.permission.SUBSCRIBED_FEEDS_READ 
android.permission.TRANSMIT_IR 
android.permission.USE_FINGERPRINT 
android.permission.VIBRATE 
android.permission.WAKE_LOCK 
android.permission.WRITE_SYNC_SETTINGS 
com.android.alarm.permission.SET_ALARM 
com.android.launcher.permission.INSTALL_SHORTCUT 
com.android.launcher.permission.UNINSTALL_SHORTCUT

Related articles: