Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have multiple instance of Service in Android

Can we have multiple instance of a Service in Android ? I want a service which can make phone silent on a specific time and I want to re use that service to make the phone in vibrate mode in specific time. So basically if I can make the service run multiple times then there is no need to create another service to set phone in vibrate mode. Also if we can run multiple instance then please explain how to stop it. Any code hint will help a lot.

like image 345
Sumit Sahoo Avatar asked Mar 26 '12 17:03

Sumit Sahoo


People also ask

Can we start service multiple times in Android?

Absolutely Correct. Only one instance of Service is created for an application process. And when you call StartService(); again, then only onStartCommand() gets called and new Intent is passed to onStartCommand() method. Note: onCreate() is not called again.

Can I run two instances of Android studio?

Go to: File -> Settings -> Appearance & Behavior -> System Settings -> Project Opening. Check [x] "Confirm window to open project in". Now open the other (2nd) project with File -> Open... etc. You will now be asked if you want to open a new window or replace what you already have.

What is an instance in Android?

An instance is a single occurrence of an event including time zone specific start and end days and minutes. The instances table is not writable and only provides a way to query event occurrences.


2 Answers

Can we have multiple instance of a Service in Android ?

No. You can have multiple subclasses of Service, but any given subclass of Service will have precisely 0 or 1 instances at any given moment.

I want a service which can make phone silent on a specific time and I want to re use that service to make the phone in vibrate mode in specific time.

You should not be using a service for any of that. Use AlarmManager and a BroadcastReceiver, please.

like image 68
CommonsWare Avatar answered Oct 11 '22 00:10

CommonsWare


I do not think the service can be used for a such purpose. Service has to provide some functionality, but only one meaning, not for several different purposes. It's better to create additional service for that to keep code simple and clear.

like image 38
Volodymyr Lykhonis Avatar answered Oct 11 '22 00:10

Volodymyr Lykhonis