CopyPastor

Detecting plagiarism made easy.

Score: 1.7878589630126953; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2016-06-03
by onkar

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;

You can check if the service is running using the code
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)
Some excerpts from hack bods response on this [Google Group Discussion][1]
If your client and server code is part of the same .apk and you are binding to the service with a concrete Intent (one that specifies the exact service class), then you can simply have your service set a global variable when it is running that your client can check.
We deliberately does not have an API to check whether a service is running because, nearly without fail, when you want to do something like that you end up with race conditions in your code.

[1]: https://groups.google.com/forum/#!topic/android-developers/jEvXMWgbgzE
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;