为什么这个 NotificationListenerService 不工作

Why is this NotificationListenerService not working

我正在与 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>

然后是服务本身:

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 e) {
            Log.e(TAG, "Error during unbind", e);
        }
        return mOnUnbind;
    }

    // onCreate is called with Android 4.4 
    // because the service is explicitly started from the MainActivity.
    // Not on Android 6 where the system binds this service itself...

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "**********  onCreate");

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

        Log.i(TAG, "**********  onNotificationPosted");
        Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i(TAG, "********** onNOtificationRemoved");
        Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
    }
}

并且主要 Activity 服务已启动,或者我们要求用户在需要时启用该设置:

if (!Utilities.hasNotificationAccess(this)) 
{
     Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
     startActivity(intent);
     Log.i(TAG,"hasNotificationAccess NO");
}
else
{
    Log.i(TAG,"hasNotificationAccess YES");

    // without this startService, it never works with Android 4.4...
    // but this is not needed in Android 6... 
    Intent mServiceIntent = new Intent(this, NLService.class);
    startService(mServiceIntent);
}

显然,通知的安全设置访问已启用...

在 Android 6 :

在 NLService 本身,添加 onBind 和 onUnbind 方法使其工作,我可以看到 onBind 日志,并且很好地触发了 onNotificationPosted。但它一直持续到应用程序的下一次启动,然后不再绑定,不再 onNotificationPosted,只是 什么都没有 发生,直到我返回安全设置以取消选中-重新检查通知访问:重复此每次启动应用程序在生产环境中是不可能的。

在 Android 4.4 上:

与 Android 6 相比,在 MainActivity 中,我需要显式启动 NLService,否则它永远不会自行统计。但是它再次适用于第一次启动并且在取消选中 - 重新检查安全设置之前永远不会再次使用...

我是不是漏掉了一些明显的东西?

Android 缓存有问题。当您在设备上上传应用程序时,OS 会将您的服务连接到通知管理器。如果您之前 运行 您的应用,Android 会在缓存中找到此服务,因此不会重新连接。

解决方法:在您的设备上推送应用程序之前,重命名您的服务(Android Studio 中的重构选项运行良好)- Android 会将此服务识别为新服务并连接到通知管理器。

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html#onListenerConnected() - 此方法将在连接到通知管理器后调用,因此您可以检查您的服务是否已连接。