如何在运行时检查权限而不抛出 SecurityException? [英] How permission can be checked at runtime without throwing SecurityException?

查看:25
本文介绍了如何在运行时检查权限而不抛出 SecurityException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个可以从 SD 获取/设置资源的函数,如果没有从 sd 中找到,则从 Asset 中获取它,如果可能,将资产写回 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_STORAGE)

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.Manifest.permission.WRITE_EXTERNAL_STORAGE;
    int res = getContext().checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);            
}

这篇关于如何在运行时检查权限而不抛出 SecurityException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆