有没有办法来检查从code清单许可?没有办法、清单、code、许可

2023-09-05 07:53:11 作者:素颜依然美、

我如何检查从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
    }