虽然授予了权限,但ContextCompat.checkSelfPermission()返回PERMISSION_DENIED

时间:2017-04-09 06:53:15

标签: android android-service android-notifications android-permissions android-service-binding

我正在尝试获得用户授予的BIND_NOTIFICATION_LISTENER_SERVICE权限。 为此,我使用以下方法在正确的位置打开设置应用程序:

Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
startActivity(intent);

这里有什么奇怪的设置是打开两次设置(如果按一下后退按钮,相同的设置屏幕会再次打开)

但是,在onResume()我然后使用以下方法检查是否已授予权限:

if(ContextCompat.checkSelfPermission(context,Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE)== PackageManager.PERMISSION_GRANTED){
   //open next activity
}

现在问题在于:用户是否在设置中授予了权限并不重要,因为checkSelfPermission()始终返回PERMISSION_DENIED

现在它变得非常奇怪:我的NotificationListenerService被实例化,绑定并完全正常工作,尽管根据checkSelfPermission()尚未授予权限。

我应该知道用户是否授予了权限?

我的清单中的许可和服务声明:

<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />

    <application>      
        <service
            android:name=".service.NotificationListener"
            android:directBootAware="true"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
    </application>

我的NotificationListenerService

public class NotificationListener extends NotificationListenerService {

    private static final String TAG = NotificationListener.class.getSimpleName();

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        Log.d(TAG, "onNotificationPosted: "+sbn.getNotification().tickerText + " ;" + sbn.getPackageName());
    }
}

我已经尝试过:

  • 不同的设备和API级别(包括模拟器) - &gt;到处都是同一个问题
  • PermissionChecker.checkSelfPermission(我不知道与ContextCompat.checkSelfPermission()相比有什么不同,但它会返回相同的结果)
  • Android Bug Tracker - &gt;没有已知问题

1 个答案:

答案 0 :(得分:0)

根据documentation权限BIND_NOTIFICATION_LISTENER_SERVICE有:

  

保护等级:签名

这意味着,只有系统应用程序才能拥有它并使用它。只有当权限具有:

时,才能通过您定义的方式请求权限
  

保护等级:危险

如果您的应用不是系统应用,并且您没有根据设备生根,可以使用自定义证书在其上安装自定义ROM(您的应用也必须使用它进行签名),那么您就是不能获得此许可。

相关问题