如何在运行时检查权限而不抛出 SecurityException?

编程入门 行业动态 更新时间:2024-10-26 04:30:57
本文介绍了如何在运行时检查权限而不抛出 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?

本文发布于:2023-11-06 04:00:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   抛出   权限   如何在   SecurityException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!