在M Preview 1之后,如果对话框显示为for the first time,则没有Never ask again复选框.
如果用户拒绝权限请求,权限对话框中会有一个Never ask again复选框"请求权限".
所以逻辑应该是这样的:
请求许可:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
} else {
//Do the stuff that requires permission...
}
判断许可是否在onRequestPermissionsResult
分钟内被拒绝或授予.
如果之前该权限被拒绝,这次权限对话框中将有一个Never ask again复选框.
拨打shouldShowRequestPermissionRationale
查看用户是否选中了Never ask again.shouldShowRequestPermissionRationale
方法仅在用户 Select 的Never ask again或设备策略禁止应用拥有该权限时返回false:
if (grantResults.length > 0){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Do the stuff that requires permission...
}else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Show permission explanation dialog...
}else{
//Never ask again selected, or device policy prohibits the app from having that permission.
//So, disable that feature, or fall back to another situation...
}
}
}
所以,你不必追踪用户是否勾选了Never ask again.