简体   繁体   English

在Android 6.0(SDK 23)中检查自定义权限

[英]Checking custom permissions in Android 6.0 (sdk 23)

I have a few questions regarding how 'custom' permissions work in Android 6.0 with the new runtime permissions. 我对使用新的运行时权限在Android 6.0中如何使用“自定义”权限有一些疑问。 For instance, a declared custom permission like this. 例如,这样声明的自定义权限。

<permission android:description="com.mycompany.myapp.DEADLY_ACTIVITY"
        android:label="string resource"
        android:name="string"
        android:protectionLevel="dangerous" />

I could not find any information online that specify how to handle this type of custom permissions at runtime in Android M. 我在网上找不到任何指定如何在Android M运行时处理这种类型的自定义权限的信息。

Should I handle and check "com.mycompany.myapp.DEADLY_ACTIVITY" permission in methods at runtime the same way I check dangerous android perimissions(camera, calendar etc.) with checkSelfPermission()? 我是否应该在运行时方法中处理和检查“ com.mycompany.myapp.DEADLY_ACTIVITY”权限,就像我使用checkSelfPermission()检查危险的Android权限(照相机,日历等)一样? and provide rationale per Google's new standards? 并根据Google的新标准提供依据?

Many Thanks! 非常感谢!

Yes. 是。 You can check the permission with the ContextCompat.checkSelfPermission method using the permission name you declared in the manifest. 您可以使用清单中声明的​​权限名称,使用ContextCompat.checkSelfPermission方法检查权限。 So if you declared this in the manifest: 因此,如果您在清单中声明了这一点:

<permission android:description="com.mycompany.myapp.DEADLY_ACTIVITY"
    android:label="string resource"
    android:name="com.mycompany.myapp.permissionName"
    android:protectionLevel="dangerous" />

then you can check the permission anywhere in the app with this: 那么您可以使用以下方法在应用程序中的任何位置检查权限:

if(ContextCompat.checkSelfPermission(context, "com.mycompany.myapp.permissionName") == PackageManager.PERMISSION_GRANTED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM