Create only one instance of Service (Android)

How can I make sure that only one instance of Service is created?

There can only be one instance of a given Service.

It gives different hash codes even when I am sure that the same function is running twice (downloading).

Then this is not the Service. Or, the service had been destroyed and recreated between logs.

And the service can run for minutes until it is completed, therefore the service can be binded to/created
by many Activities

Then the Service is probably being destroyed and recreated. If you need the service to run for minutes, you need to use startService() and stopSelf() in addition to your bindService() and unbindService() calls. Or, perhaps you do not need to bind at all, in which case you might consider using an IntentService, since that automatically gives you a background thread on which to do your downloads.

Leave a Comment