我在没有太多运气的情况下与NotificationListenerService作斗争.我尝试了很多在这里和那里找到的食谱,特别是这样......但它的作用相当不一致.
首先看一下代码:
清单:
<service
android:name=".services.NLService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
那么服务本身:
public class NLService extends NotificationListenerService {
private String TAG = "NLService";
// bind and unbind seems to make it work with Android 6...
// but is never called with Android 4.4...
@Override
public IBinder onBind(Intent mIntent) {
IBinder mIBinder = super.onBind(mIntent);
Log.i(TAG, "onBind");
return mIBinder;
}
@Override
public boolean onUnbind(Intent mIntent) {
boolean mOnUnbind = super.onUnbind(mIntent);
Log.i(TAG, "onUnbind");
isNotificationAccessEnabled = false;
try {
} catch (Exception …Run Code Online (Sandbox Code Playgroud) 我创建了一个应用程序并添加了一个扩展“NotificationListenerService”的 java 类。一切正常,但我无法获得 BIND_NOTIFICATION_LISTENER_SERVICE 的许可。我在清单中添加了它,但是当我检查权限时:
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE)
!= PackageManager.PERMISSION_GRANTED)
{
Log.d(TAG, "permission denied");
}
else
Log.d(TAG, "permission granted");
Run Code Online (Sandbox Code Playgroud)
但我一直被拒绝许可。我知道此权限不是“危险权限”,因此我不必向用户索取。在清单中,我声明了这一点:
<service android:name=".PcNotification"
android:label="PCNotificationService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
它仍然不起作用。我也实现了这个功能,onListenerConnected但它从未调用过,所以这意味着我的服务永远不会连接到通知管理器,可能是因为我没有权限 BIND_NOTIFICATION_LISTENER_SERVICE
我只想知道如何向我的应用授予此权限。