gpt4 book ai didi

android - 如何正确地动态检查android权限

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:51:06 25 4
gpt4 key购买 nike

选择题:

以下哪项将正确检查应用程序是否具有在其 AndroidManifest.xml 中声明的特定权限?

getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

getContext().getPackageManager().checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED

基本上,我对 android 文档中关于 checkCallingOrSelfPermission 声明的内容感到害怕 -> 如果您只是检查它,它就会授予 IPC 权限??那有什么意思? http://developer.android.com/reference/android/content/Context.html#checkCallingOrSelfPermission(java.lang.String)

所以任何关于真正差异的解释都很棒 :D

*注意:我在库中提供此代码,因此我只能在运行时检查权限,除非您知道更好的方法。

最佳答案

根据我的理解(这可能是错误的,因为我还没有太多使用 IPC):

鉴于您的代码是从另一个应用程序执行的(例如,您的库未编译到应用程序中,而是使用 Binder 或类似的东西暴露给第三方),您可以使用 checkCallingPermission检查第三方应用程序是否具有给定的权限,而 checkCallingOrSelfPermission包括您的库被编译到的应用程序的权限。

您需要单独处理调用者的权限,因为在检查自己的权限时也可能将权限泄露给其他应用程序。来自security tips :

Do not leak permission-protected data. This occurs when your app exposes data over IPC that is only available because it has a specific permission, but does not require that permission of any clients of it’s IPC interface.

[...]

If providing an interface that does require access controls, use checkCallingPermission() to verify whether the caller has a required permission. This is especially important before accessing a service on behalf of the caller, as the identify of your application is passed to other interfaces.

您描述的包管理器方式检查您的库编译到的应用程序的权限。

因此,如果您的代码不是从另一个进程执行的,您可能不必关心差异。否则,使用包管理器方式或 clear the calling identity stuff如果您对是否可以执行某项任务感兴趣;如果您希望检查调用进程是否也可以执行该任务,请另外检查调用者的权限。

关于android - 如何正确地动态检查android权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18393175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com