Android 确定是否具有NotificationListener所需的权限。安卓

Android 确定是否具有NotificationListener所需的权限。安卓,android,permissions,android-permissions,Android,Permissions,Android Permissions,我有一项服务: public class NotificationCatcher extends NotificationListenerService 已在清单中声明权限,如: <service android:name=".tasks.NotificationCatcher" android:enabled="true" android:exported="true" android:permission="androi

我有一项服务:

public class NotificationCatcher extends NotificationListenerService
已在清单中声明权限,如:

<service
        android:name=".tasks.NotificationCatcher"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

如何确定是否授予了PackageManager.PERMISSION?当我无法使用
检查CallingorSelfPermission(permission)
时,因为我没有
上下文
。 当我使用
MainActivity
中的
Context
时,会出现异常,我的Android应用程序崩溃

我想要达到的目标:
main活动
确定是否具有
NotificationCatcher
android.permission。绑定通知侦听器服务
,然后执行操作。

您可以在服务中获取上下文


调用getApplicationContext()函数。

我已经尝试过了,结果是java.lang.NullPointerException不幸没有