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; }
source share