ActivityManager # getRunningServices returning a service that is manually stopped

activityManager.getRunningServices(Integer.MAX_VALUE); 

This method returns a list that includes a service that is manually stopped.

Settings> Applications> Service Execution

+4
source share
1 answer

All approaches using onDestroy or onSometing events, or Binders or static variables will not work reliably because, as a developer, you never know when Android decides to kill you, or which of the mentioned callbacks are called or not. Note the "killable" column in the lifecycle event table in the Android documentation.

// use this code to check if your service is working or not

 private boolean isMyServiceRunning() { ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if ("com.example.MyService".equals(service.service.getClassName())) { return true; } } return false; } 
+2
source

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


All Articles