The introduction
After Android 6.0 (API 23), the system began to manage Android permissions. Some permissions have been changed to apply for at runtime, rather than being listed at application installation, which means the user has agreed to all permissions once they are successfully installed. The downside of this is that some rogue apps can grab users’ sensitive private information, so in Android 6.0, Google finally made some dangerous permissions require users’ consent to take effect.
Classification of permissions
Permissions can be classified into normal permissions and dangerous permissions:
1. Normal permissions
Normal permissions are those that do not directly risk user privacy permissions. If we list the normal permissions in AndroidManifest, the system will automatically grant the permissions to our application when it is installed.
2. Danger permission
Dangerous permissions are those that involve user privacy. These permissions are listed in AndroidManifest, but also need to be manually granted by the user when the application is installed or used.
test
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); / / to detect whether there is the permissions int permission = ContextCompat. CheckSelfPermission (this, the Manifest. Permission. WRITE_EXTERNAL_STORAGE);if(permission ! = PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);if(! ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Toast.makeText(this,"No more reminders checked, need to go to Settings page", Toast.LENGTH_SHORT).show(); }}else{// Already obtained permissions, Perform the corresponding operation}} @ Override public void onRequestPermissionsResult (int requestCode, @ NonNull String [] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) {case 1:
if(grantResults. Length > 0 && grantResults [0] = = PackageManager. PERMISSION_GRANTED) {/ / already obtained permission, perform the corresponding operation}else{// Permission denied, you can play a custom popup to remind the user, permission denied cannot use certain functions}break; }}}Copy the code
The first step: To determine whether a user to our authorization, we use ContextCompat. CheckSelfPermission method, the first parameter is the Context, the second parameter is the specific name of permissions, The return value of this method needs and PackageManager. PERMISSION_GRANTED comparison, if equal means the permission has been granted, if not equal rights has not been granted. The second step: For permission is not granted, we need to use ActivityCompat. RequestPermissions method to apply for the permission, the first parameter is the Activity instance, the second parameter is to apply for permission to array, the third parameter is the request code, The request code needs to be consistent with the callback method onRequestPermissionsResult below. After call ActivityCompat. RequestPermissions method, the system will pop up a dialog box, permission to apply for the user can choose agree or refused permission application, whether agree or reject, can callback onRequestPermissionsResult method, The results of authorization are encapsulated in grantResults.