2

I want to implement a Service that acts like a Singleton.

I need my App to check if the screen is locked, then start a Listener IntentService. If the screen becomes unlocked, it will kill the Listener service. I created a ScreenLockService that is an IntentService and is called by the Home activity if the user checked the on/off box because I want the Listener to run even if the the App isn't running.

So the Home activity runs and stops the ScreenLockService service which will run and stop the Listener service.

My problem is that the Home Activity creates multiple instances of ScreenLockService which also creates multiple instances of Listener.

So I'd like to have these two IntentServices run as Singletons, but I'm not sure how to do so.

I've checked out a bunch of tutorials, but they're either out dated or not tailored to IntentServices such as: http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/ Singleton in Android

I can post some example code of what I have if necessary, but all I'm really looking for is a how-to.

Community
  • 1
  • 1
Ausche
  • 139
  • 2
  • 12
  • 5
    a `Service` is a singleton: you cannot have two instances of the same `Service` – pskink May 31 '16 at 20:51
  • Are you stopping and starting the service in onPause, onResume of your activity? – Pablo May 31 '16 at 21:19
  • @Pablo No, I'm creating the `ScreenLockService` intent and starting it in the onCreate of the `Home` Activity. Then if the screen is locked, I start the `Listener` service. – Ausche Jun 03 '16 at 23:55
  • @pskink Maybe I mean something different. When I debug and monitor the threads (one is created in each service), which I've named `ScreenLockService` and `Listener` respectively, I see multiple threads of the same name popping up when I just need one to be active. I can do this, but maybe I need a way to make a `Thread` a singleton, not the service class? – Ausche Jun 04 '16 at 00:00
  • sorry, without your code i have no idea what you mean – pskink Jun 04 '16 at 03:21

1 Answers1

2

Well the basic idea for implementation of the Singleton patterns is to make a private [or protected] constructor (which means it will not be accessible from outside the class). The class also needs a private field of it's own type (e.g. private MyClass myObj;) Next thing you need is the public static method called something like newInstance() which returns the instance of the class via myObj if it's not null, and instantiate it if it is:

public static MyClass newInstance(){
    if (myObj == null) {
         myObj = new MyClass();
    }
return myObj;
}

Of course it can have a more complex implementation (the constructor does not have to be without parameters).

Now this will always create exactly one instance of the desired class. All you need to do is to always call the method newInstance in the place where you need to use your class. If it was already instantiated, it will give you the old instance, if not, it'll create it.

Hope this was the explanation you were looking for.

Vucko
  • 7,371
  • 2
  • 27
  • 45
  • 2
    This answer explains how to implement a java singleton (generally). The question was about implementing Android Servicies as singleton. – Pablo May 31 '16 at 21:16
  • Probably the same way, I don't see why it'd differ. – Vucko May 31 '16 at 21:29
  • Like @pskink said, you can't have two instances of the same service. – Pablo May 31 '16 at 21:39
  • @Vucko Thanks for the response, but I'm going to look into making _threads_ singletons, not services as I think pskink and Pablo are right. However my issue remains where I'm getting multiple threads being made. – Ausche Jun 04 '16 at 00:02
  • A service by default works as a Singleton. There is no possible way you can create another instance of a service if it is running. – Rohit Singh Apr 09 '19 at 13:05