Android M - anyway to know if a user has chosen ne

2019-09-09 09:32发布

问题:

If an all calls ActivityCompat.requestPermissions() then the OS displays a dialog with Deny or Permit buttons. There is also a never show this again tick box.

However there's no way that I can see for the app to know if the user has ticked this box - within onRequestPermissionsResult() the grantResults parameter is PERMISSION_DENIED if the user clicks Deny and also if the user ticks the tick box before clicking Deny. So is there anyway an app can find out if the user doesn't want to see that dialog again?

回答1:

You can use ActivityCompat.shouldShowRequestPermissionRationale() - it will return false in a few cases:

  • You've never asked for the permission before
  • The user has checked the 'never again' checkbox
  • The permission has been disabled by policy (say, in a work situation)

By combining this with a shared preference to store if you've ever asked for permission, you can effectively determine if they'll actually see the dialog when you call requestPermissions().