CopyPastor

Detecting plagiarism made easy.

Score: 0.8321437934159183; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2020-09-08
by Syed Rafaqat Hussain

Original Post

Original - Posted on 2011-05-07
by geekQ



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I use the following from inside an activity:
private boolean isMyServiceRunning(Class<?> serviceClass) { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceClass.getName().equals(service.service.getClassName())) { return true; } } return false; }
And I call it using:
isMyServiceRunning(MyService.class) This works reliably, because it is based on the information about running services provided by the Android operating system through [ActivityManager#getRunningServices][1].
All the 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 your process or which of the mentioned callbacks are called or not. Please note the "killable" column in the [lifecycle events table][2] in the Android documentation.

[1]: http://This%20works%20reliably,%20because%20it%20is%20based%20on%20the%20information%20about%20running%20services%20provided%20by%20the%20Android%20operating%20system%20through%20ActivityManager#getRunningServices. [2]: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
I use the following from inside an activity:
private boolean isMyServiceRunning(Class<?> serviceClass) { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceClass.getName().equals(service.service.getClassName())) { return true; } } return false; }
And I call it using:
isMyServiceRunning(MyService.class)
This works reliably, because it is based on the information about running services provided by the Android operating system through [ActivityManager#getRunningServices][1].
All the 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 your process or which of the mentioned callbacks are called or not. Please note the "killable" column in the [lifecycle events table][2] in the Android documentation.
[1]: http://developer.android.com/reference/android/app/ActivityManager.html#getRunningServices%28int%29 [2]: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

        
Present in both answers; Present only in the new answer; Present only in the old answer;