Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

android - Why is this NotificationListenerService not working

I'm fighting against the NotificationListenerService without much luck. I tried a lot of recipes found here and there, especially on so... But it works quite inconsistently.

A first look at the code :

Manifest :

<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>

Then the service itself :

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() + "" + sbn.getNotification().tickerText + "" + sbn.getPackageName());
    }

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

And in the main Activity the service is started or we ask the user to enable the setting if needed :

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);
}

Obviously, the secure setting access for notifications is enabled...

On Android 6 :

On the NLService itself, the addition of the onBind and onUnbind methods make it work, I can se the onBind log, and the onNotificationPosted is triggered nicely. But it lasts until the next launch of the app, then no more binding, no more onNotificationPosted, just nothing happens until I go back to security settings to uncheck-recheck the notifications access: repeating this each time the App is started is not something possible in a production environment.

On Android 4.4 :

In contrast to Android 6, in the MainActivity I need to explicitly start the NLService otherwise it never stats by itself. But once again it works for the first launch and never works again until uncheck-recheck the security setting...

Did I miss something obvious ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a problem with Android caching. When you upload app on the device, OS connects your service to Notification Manager. If you ran your app before, Android founds this service in cache so it's not reconnected.

Workaround: before pushing the app on your device, rename your service (refactor option in Android Studio works well) - Android will recognize this service as new one and connect to Notification Manager.

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html#onListenerConnected() - this method will be invoked after connecting to Notification Manager so you can check if your service is connected or not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...