Forked from xinghui/NotificationCollectorMonitorService.java
Created
October 18, 2023 03:55
-
-
Save takeseem/d28472387ea92bb9571fc847ca1cf07b to your computer and use it in GitHub Desktop.
Cannot get the NotificationListenerService class to work http://stackoverflow.com/questions/17911883/cannot-get-the-notificationlistenerservice-class-to-work -----&----- https://www.zhihu.com/question/33540416/answer/113706620
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment