Skip to content

Instantly share code, notes, and snippets.

@xinghui
Last active March 16, 2024 13:39
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save xinghui/b2ddd8cffe55c4b62f5d8846d5545bf9 to your computer and use it in GitHub Desktop.
Save xinghui/b2ddd8cffe55c4b62f5d8846d5545bf9 to your computer and use it in GitHub Desktop.
package com.xinghui.notificationlistenerservicedemo;
import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.Process;
import android.util.Log;
import java.util.List;
/**
* Created by xinghui on 9/20/16.
* <p>
* calling this in your Application's onCreate
* startService(new Intent(this, NotificationCollectorMonitorService.class));
* <p>
* BY THE WAY Don't Forget to Add the Service to the AndroidManifest.xml File.
* <service android:name=".NotificationCollectorMonitorService"/>
*/
public class NotificationCollectorMonitorService extends Service {
/**
* {@link Log#isLoggable(String, int)}
* <p>
* IllegalArgumentException is thrown if the tag.length() > 23.
*/
private static final String TAG = "NotifiCollectorMonitor";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate() called");
ensureCollectorRunning();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
private void ensureCollectorRunning() {
ComponentName collectorComponent = new ComponentName(this, /*NotificationListenerService Inheritance*/ NotificationCollectorService.class);
Log.v(TAG, "ensureCollectorRunning collectorComponent: " + collectorComponent);
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
boolean collectorRunning = false;
List<ActivityManager.RunningServiceInfo> runningServices = manager.getRunningServices(Integer.MAX_VALUE);
if (runningServices == null ) {
Log.w(TAG, "ensureCollectorRunning() runningServices is NULL");
return;
}
for (ActivityManager.RunningServiceInfo service : runningServices) {
if (service.service.equals(collectorComponent)) {
Log.w(TAG, "ensureCollectorRunning service - pid: " + service.pid + ", currentPID: " + Process.myPid() + ", clientPackage: " + service.clientPackage + ", clientCount: " + service.clientCount
+ ", clientLabel: " + ((service.clientLabel == 0) ? "0" : "(" + getResources().getString(service.clientLabel) + ")"));
if (service.pid == Process.myPid() /*&& service.clientCount > 0 && !TextUtils.isEmpty(service.clientPackage)*/) {
collectorRunning = true;
}
}
}
if (collectorRunning) {
Log.d(TAG, "ensureCollectorRunning: collector is running");
return;
}
Log.d(TAG, "ensureCollectorRunning: collector not running, reviving...");
toggleNotificationListenerService();
}
private void toggleNotificationListenerService() {
Log.d(TAG, "toggleNotificationListenerService() called");
ComponentName thisComponent = new ComponentName(this, /*getClass()*/ NotificationCollectorService.class);
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
@xinghui
Copy link
Author

xinghui commented Sep 22, 2016

Zombie service:

service - uid: 10069,
clientPackage: null,
process: com.xinghui.notificationlistenerservicedemo,
activeSince: 1276509,
clientCount: 0,
clientLabel: 0,
crashCount: 0,
describeContents: 0,
flags: 0,
foreground: false,
lastActivityTime: 1276519,
pid: 0,
restarting: 0,
started: false

Correct service:

service - uid: 10069,
clientPackage: android,
process: com.xinghui.notificationlistenerservicedemo,
activeSince: 1276509,
clientCount: 1,
clientLabel: 17040570,
crashCount: 0,
describeContents: 0,
flags: 0,
foreground: false,
lastActivityTime: 1287852,
pid: 25011,
restarting: 0,
started: false

@jareddlc
Copy link

jareddlc commented Oct 9, 2016

thanks @xinghui

@zoneguo
Copy link

zoneguo commented Jan 9, 2017

good

@nujnay
Copy link

nujnay commented Oct 12, 2017

thx

@justinvdk
Copy link

Is this still supposed to work with 8.0? I'm still having trouble with the service becoming stale after updates.

@Vel-San
Copy link

Vel-San commented Jan 21, 2018

@justinvdk Plus that, service dies even when NotificationCollectorMonitorService is still alive; i checked the services from developer settings and added logs, NotificationCollectorMonitorService is always alive but my service is dead.

I've found a possible fix though, for anyone having this issue Check this post and read the comments

@Chuque
Copy link

Chuque commented May 12, 2018

Worked on my xiaomi mi 6 running android 7.1.1

@terrelewis
Copy link

This method seems to stop working after it's called a few number of times. Is this expected?

@UdiOshi85
Copy link

Not working for me. OnePlus 5T Android 8.1
When service is "dead" I cannot re-enable it. also tried with:
NotificationListenerService.requestRebind(ComponentName(this, serviceClass::class.java))

@emrahdk
Copy link

emrahdk commented Nov 21, 2019

Working for me on Android 9 with Xiaomi Mi2. Thanks @xinghui 🎉

@panovvitalik
Copy link

Not working for me

@Mr-Ramzan
Copy link

Mr-Ramzan commented Jul 29, 2020

it works for a while then stops Can anyone help please
Android 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment