如何权限的用户可以在运行时进行检查,没有抛出SecurityException异常?用户可以、抛出、异常、权限

2023-09-12 01:05:57 作者:‖突然七年似尘埃

我设计出可以得到一个函数/从标清集资源,如果没有找到从SD然后从它的资产有可能的话写在资产回SD 此功能可以当SD安装,并通过方法调用检查访问...

I design a function that may get/set a resource from SD and if not found from sd then take it from Asset and if possible write the asset back to SD This function may check by method invocation if SD is mounted and accessible...

boolean bSDisAvalaible = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

我的设计功能可用于从一个应用程序(项目)到另一个(带或不带android.permission.WRITE_EXTERNAL_STORAG​​E)

My designed function may be used from one app(project) to another (with or without android.permission.WRITE_EXTERNAL_STORAGE)

然后,我想检查当前应用程序有这个特殊的权限而不SecurityException异常播放。

Then I would like to check if the current application has this particular permission without playing with SecurityException.

是否存在一个好的方式在运行时咨询当前定义的权限?

Does it exist a "nice" way to consult current defined permissions at runtime ?

推荐答案

您可以使用 Context.checkCallingorSelfPermission()功能这一点。下面是一个例子:

You can use Context.checkCallingorSelfPermission() function for this. Here is an example:

private boolean checkWriteExternalPermission()
{

    String permission = "android.permission.WRITE_EXTERNAL_STORAGE";
    int res = getContext().checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);            
}