Verification service works or not

I have a service that, when I start it, I plan to work with the alarm manager at 00:00, I want to check if my service is working or not?

when I check Settings โ†’ Applications โ†’ Launch tasks, My service exists in a cached process, so the code below does not work, because my service, which marks when the service starts, can someone help me?

private boolean isServiceRunning(String serviceName) { ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50); Iterator<ActivityManager.RunningServiceInfo> i = l.iterator(); while (i.hasNext()) { ActivityManager.RunningServiceInfo runningServiceInfo = (ActivityManager.RunningServiceInfo) i.next(); if( runningServiceInfo.service.getClassName().equals(serviceName) ) { return true; } } return false; } 
+4
source share
2 answers

BindService returns false if you do not succeed in binding to the service:

Returns

If you are successfully attached to the service, true is returned; false is returned if no connection is made, so you will not receive a service object.

I would use this to check if the service is running.

Just remember to bind the AUTO_CREATE flag or create a service.

+2
source

I use the following from within the action:

 private boolean isMyServiceRunning() { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (MyService.class.getName().equals(service.service.getClassName())) { return true; } } return false; } 
+1
source

Source: https://habr.com/ru/post/1403773/


All Articles