Java checkSelfPermission与&;应该显示请求许可的基本原理吗?
我有点困惑,Java checkSelfPermission与&;应该显示请求许可的基本原理吗?,java,android-permissions,Java,Android Permissions,我有点困惑,checkSelfPermission()和shouldshowrequestpermissionrational() 如果我理解正确,它们都会检查应用程序是否需要向用户请求权限,那么为什么我要检查两次呢?为什么不能这么简单呢 if (ShouldAskForPermission(WRITE_FILE) == true) ShowMessage("The application need permission for writing a file on your device,
checkSelfPermission()
和shouldshowrequestpermissionrational()
如果我理解正确,它们都会检查应用程序是否需要向用户请求权限,那么为什么我要检查两次呢?为什么不能这么简单呢
if (ShouldAskForPermission(WRITE_FILE) == true)
ShowMessage("The application need permission for writing a file on your device, do you allow it?", YES_NO_BUTTONS);
在M Preview 1之后,如果第一次显示该对话框,则不存在“永不再次询问”复选框 如果用户拒绝权限请求,则在第二次请求权限时,权限对话框中将出现“永不再次请求”复选框 所以逻辑应该是这样的: 请求许可:
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...
}
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...
}
}
}
检查onRequestPermissionsResult中的权限是否被拒绝或授予。如果以前拒绝了该权限,则这次在“权限”对话框中将出现“永不再次询问”复选框
调用shouldShowRequestPermissionRegulation查看用户是否选中了Never ask。shouldShowRequestPermissionRegulation方法仅在用户选择“永不再次询问”或设备策略时返回false
禁止应用程序拥有该权限:
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...
}
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...
}
}
}
因此,您不必跟踪用户是否选中了“永不再问”或“不问”。“他们都会检查应用程序是否需要向用户请求权限”。不,它们检查不同的内容:
checkSelfPermission
用于检查您的应用程序当前是否具有某些权限ShouldShowRequestPermissionRegulation
用于检查您是否应该向用户显示自定义消息,解释您的应用程序需要权限的原因,至少在最近的Android版本中,只有当用户之前拒绝您的应用程序该权限时,才会出现这种情况。这仍然没有意义,为什么showldRequestPermissionRegulation()不能同时检查我想要的函数是否有权限?(例如访问设备文件)如果有权限,请告诉我不必显示解释消息?为什么不能全部放在一个函数中呢?因为第一次请求权限时,这两个函数返回的结果并不相同ShouldShowRequestPermissionRegulation
基本上可以跟踪用户是否拒绝过您的应用程序的许可,因此您不必自己这样做checkSelfPermission
只返回当前授予状态。为什么像ShouldTheAppAskForPermission(CAMERA\u ACCESS)这样的函数不能只有在1时才返回“true”。这是应用程序第一次运行,2。我们需要许可才能进入摄像机,3。如果用户拒绝访问之前运行的应用程序上的摄像头,但他没有要求不再问他。为什么这样的功能还不够?如果函数返回'true',我将询问用户权限并解释原因,如果函数返回'false',我将不询问用户权限。因为“我将询问用户权限并解释原因”不是谷歌设计的方式。预期的流程是,在请求用户已拒绝至少一次的权限之前,将向用户显示仅包含解释的额外对话框。实际的权限请求对话框是一个系统对话框,您不能在其中添加任何自己的文本。您可以附加原始答案的链接,而不是在此处复制。