在运行时请求 Android 权限时的边界情况
Borderline cases when requesting Android permissions at runtime
在 Android >= 6:
当调用需要权限 P 的方法但未在清单中声明权限 P 时,将引发 java.lang.SecurityException
。
当我不检查是否授予危险权限并调用使用它的方法时,会发生相同的异常。
当你在运行时使用ActivityCompat.requestPermissions
方法请求一个危险权限P,但清单中没有声明危险权限P时,对话框不会出现并且默认情况下拒绝该权限(并且您看不到日志中发生的情况)。
这些说法对吗?
When a method, that requires a permission P, is invoked but the permission P isn't declared in the manifest a java.lang.SecurityException will be raised.
大体正确。可能有一些边缘情况会引发其他异常。权限保护的行为将无法正常工作。
The same exception occurs when I doesn't check if a dangerous permission was granted and I invoke a method that uses it.
仅适用于 Android 6.0+,适用于 targetSdkVersion
23 或更高版本的应用程序。
When you ask for a dangerous permission P at runtime with the ActivityCompat.requestPermissions method, but the dangerous permission P isn't declared in the manifest, the dialog box doesn't appear and the permission is denied by default (and you can't see that this happened looking the logs).
"denied by default" 部分与您的第一个问题相同。否则,你是对的——你只能为你在清单中请求的那些 dangerous
请求运行时权限。
在 Android >= 6:
当调用需要权限 P 的方法但未在清单中声明权限 P 时,将引发
java.lang.SecurityException
。当我不检查是否授予危险权限并调用使用它的方法时,会发生相同的异常。
当你在运行时使用
ActivityCompat.requestPermissions
方法请求一个危险权限P,但清单中没有声明危险权限P时,对话框不会出现并且默认情况下拒绝该权限(并且您看不到日志中发生的情况)。
这些说法对吗?
When a method, that requires a permission P, is invoked but the permission P isn't declared in the manifest a java.lang.SecurityException will be raised.
大体正确。可能有一些边缘情况会引发其他异常。权限保护的行为将无法正常工作。
The same exception occurs when I doesn't check if a dangerous permission was granted and I invoke a method that uses it.
仅适用于 Android 6.0+,适用于 targetSdkVersion
23 或更高版本的应用程序。
When you ask for a dangerous permission P at runtime with the ActivityCompat.requestPermissions method, but the dangerous permission P isn't declared in the manifest, the dialog box doesn't appear and the permission is denied by default (and you can't see that this happened looking the logs).
"denied by default" 部分与您的第一个问题相同。否则,你是对的——你只能为你在清单中请求的那些 dangerous
请求运行时权限。