2

I have a service which is used by many application. Developers just start the service and it does its work. If AppA, AppB and AppC start the service I am getting duplicates of my services. Duplicates are not so bad, in fact there should be duplicates of service, but services should do their work if and only if none of duplicates are already doing same job. My problem is, checking inside service wether its runing looks like against logic because if you start service, of course its already runing and it makes no sense to check. Service is by the way STICKY. I want to keep duplicates, if service that does job gets killed, another one (one of duplicates) will start do the job, since none of services doing it. Is there any work around? How could I achieve that? Please if something is unclear, let me know.

  • How did you know about duplicate service?? – Pankaj Kumar Aug 04 '14 at 13:48
  • How about keeping a static reference to the service object and checking if it is null or not? – AndroidDev Aug 04 '14 at 13:48
  • @PankajKumar in Eclipse -> Devices I can see that AppA is runing and ServiceA and AppB is runing and ServiceA again. –  Aug 04 '14 at 13:53
  • @Harish could you please explain it in details? –  Aug 04 '14 at 13:55
  • "but services should do their work if and only if none of duplicates are already doing same job" -- what is the trigger mechanism for the work? IOW, what is causing *any* of these services to start up and do something? If you're trying to have some instance of this service effectively run forever, [only have a service running when it is actively delivering value to the user](http://commonsware.com/blog/2014/07/27/role-services.html). – CommonsWare Aug 07 '14 at 20:43
  • Thats the problem. Nothing causes any service to start up. You just simply start up the service inside onCreate() of main activity. I want to trigger services inside of themselves. –  Aug 07 '14 at 20:51
  • "I want to trigger services inside of themselves" -- sorry, but that does not make any sense to me. Why does the service even exist? What benefit does it deliver to the user (let alone the developer) that warrants anyone starting it up in `onCreate()`? And, since the service should only be running while it is actively delivering value to the user, what will cause the service to eventually stop? – CommonsWare Aug 07 '14 at 20:56
  • A service exists, lets say to deliver you a message "hello". And there are many same services on your device. In case it delivers you a message on some events. You would get N messages where N is number of running same services. But you dont want that. You want to trigger services emselves, cuz if any of em already listening for events and goin to deliver you a message, it makes no sense for the others to do same job again. It would be also annoying if device says you "hello" N times. "What will cause the service to stop?" - Either a mad user or low memory. If one stops, another says "hello". –  Aug 07 '14 at 21:08

1 Answers1

0
public class PlayerService extends Service {

    /** The Singleton PlayerService instance. */
    private static PlayerService _instance = null;

    @Override
    public void onCreate() {
        _instance = this;
    }

    public static boolean isPlayerServiceRunning() {
        return _instance != null;
    }
AndroidDev
  • 5,193
  • 5
  • 37
  • 68
  • Looks like it doesnt work. Both service were started and both are doing the same job. –  Aug 04 '14 at 14:14
  • The other posts also suggest the same (http://stackoverflow.com/a/608600/391401). Post your code then we can check if you are doing anything different. – AndroidDev Aug 04 '14 at 14:46
  • Here is same solution I found (http://stackoverflow.com/a/6070918/2917245) at the end of the answer: "This solution is elegant but only relevant if you have access to the service class and **only for classes iside the app of the service.**" so this one isnt useful me. Because I am trying to find out wether any other applications, which are outside the app of current service, has already started same service, which is tryin to determine it. –  Aug 04 '14 at 16:09
  • How about http://developer.android.com/reference/android/app/ActivityManager.html#getRunningServices(int) then? – AndroidDev Aug 04 '14 at 16:55