有没有办法来检查从code清单许可? [英] Is there a way to check for manifest permission from code?

查看:104
本文介绍了有没有办法来检查从code清单许可?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查从code中的manifest.xml的特定权限?我想如果一些权限的necessay我的应用程序缺少抛出一些异常。例如: FINE_LOCATION COARSE_LOCATION 我知道Android还将对正在使用启动特定activiy的抛出异常GPS,但我需要检查清单,并在启动该应用程序本身抛出异常。这不仅对位置的访问,同时也为其他权限。任何建议将是有益的。

How do I check for a specific permission in the manifest.xml from code? I want to throw some exception if some permissions that are necessay for my application are missing. for example FINE_LOCATION and COARSE_LOCATION I know that android will also throw an exception on the launch of the specific activiy that is using GPS, but I need to check the manifest and throw an exception at the launch of the application itself. This holds not only for location access, but also for other permissions. Any suggestions would be helpful.

推荐答案

您可以检查通过的 PackageManager 的权限是否被授予或不特定的许可。例如

You can check whether the permission is granted or not for specific permission by using PackageManager. For example

    PackageManager pm = getPackageManager();
    if (pm.checkPermission(permission.FINE_LOCATION, getPackageName()) == PackageManager.PERMISSION_GRANTED) {
        // do something
    } else {
        // do something
    }

这篇关于有没有办法来检查从code清单许可?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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