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

后端 未结 10 655
萌比男神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条回答
  •  离开以前
    2020-11-28 21:14

    Regarding MLProgrammer-CiM's answer, I have an idea of how to solve the scenario in which the user revokes the permission after the boolean stored in SharedPrefrences is already true,

    simply create another constant boolean, if the first one called for example: Constant.FIRST_TIME_REQUEST (which its default state will be true) the second one will be called Constant.PERMISSION_ALREADY_GRANTED (which will be false on default)

    On onRequestPermissionsResult if permission was granted you change its value to true, of course.

    Now, in the part where you want to ask permission with pre-explanation, write something like that:

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
       SharedPreferences sp = context.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
       boolean isPermissionGranted = sp.getBoolean(Constant.PERMISSION_ALREADY_GRANTED, false);
       if (isPermissionGranted) {
          sp.putBoolean(Constant.PERMISSION_ALREADY_GRANTED, false);
          sp.putBoolean(Constant.FIRST_TIME_REQUEST, true);
       }
    
       if (ActivityCompat.shouldShowRequestPermissionRationale(activity, androidPermissionName) || sp.getBoolean(Constant.FIRST_TIME_REQUEST, true) ) {
       showDialogExplanation();
    }
    }
    

    that way even if the user will remove the permission, the boolean will be set to false again.

    good luck, I hope it'll help.

    Shlo

提交回复
热议问题