How Do We Distinguish Never-Asked From Stop-Asking in Android M's Runtime Permissions?

后端 未结 10 681
萌比男神i
萌比男神i 2020-11-28 20:48

When it comes to the M Developer Preview runtime permissions, according to Google:

  1. If you have never asked for a certain permission before, just ask for it<

10条回答
  •  旧时难觅i
    2020-11-28 21:18

    After trying all the answer here and some other post over internet. I came to know that I have to use a sharedPreference isLocationPermissionDialogShown (default false) and every thing works as per expected.

    1. If first time asked for permission. In this case shouldShowRequestPermissionRationale returns false and isLocationPermissionDialogShown also false
    2. Second time shouldShowRequestPermissionRationale return true and while showing dialog we set isLocationPermissionDialogShown to true. and when we check condition both will be true
    3. Every Time until Never Ask Again ticked shouldShowRequestPermissionRationale return true and isLocationPermissionDialogShown returns true
    4. If Never Ask Again ticked shouldShowRequestPermissionRationale return false and isLocationPermissionDialogShown returns true. Which is what we need.

    Please check working example.

    public class MainActivity extends AppCompatActivity {
        SharedPreferences sharedPreferences;
        String locationPermission;
        String prefLocationPermissionKey = "isLocationPermissionDialogShown";
        private final int PERMISSION_REQUEST_CODE_LOCATION = 1001;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            locationPermission = Manifest.permission.ACCESS_FINE_LOCATION;
            sharedPreferences = getSharedPreferences("configuration", MODE_PRIVATE);
    
            //check for android version
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //Check for permission
                if (checkSelfPermission(locationPermission) != PackageManager.PERMISSION_GRANTED) {
                    //check if clarification dialog should be shown.
                    if (shouldShowRequestPermissionRationale(locationPermission)) {
                        showClarificationDialog(locationPermission, PERMISSION_REQUEST_CODE_LOCATION);
                    } else  {
                        requestPermissions(new String[] { locationPermission}, PERMISSION_REQUEST_CODE_LOCATION);
                    }
                } else {
                    Log.d("nets-debug", "permission already grranted");
                }
            }
    
        }
    
        @Override
        @TargetApi(Build.VERSION_CODES.M)
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    
            if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                //for location permission
                if (requestCode == PERMISSION_REQUEST_CODE_LOCATION) {
                    boolean isLocationPermissionDialogShown = sharedPreferences.getBoolean(prefLocationPermissionKey, false);
    
                    if (!shouldShowRequestPermissionRationale(locationPermission) && isLocationPermissionDialogShown) {
                        // user selected Never Ask Again. do something
                        Log.d("nets-debug", "never ask again");
                    } else {
                        // all other conditions like first time asked, previously denied etc are captured here and can be extended if required.
                        Log.d("nets-debug", "all other cases");
                    }
                }
    
            }
    
        }
    
        @TargetApi(Build.VERSION_CODES.M)
        public void showClarificationDialog(final String permission, final int requestCode) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Permission Required");
            builder.setMessage("Please grant Location permission to use all features of this app");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean(prefLocationPermissionKey, true);
                    editor.apply();
                    requestPermissions(new String[] {permission}, requestCode);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "This permission required", Toast.LENGTH_LONG).show();
                }
            });
            builder.create().show();
        }
    
    }
    

    Hope this will help.

提交回复
热议问题