I have started a service in Activity.Now, if i force close the activity then automatically the service is also force stopped.Is there any way to restart it automatically.? Thanks in Advance.
Asked
Active
Viewed 7,046 times
0
-
Using Alarm manager you can try or broadcast receiver you can try.. – Arindam Mukherjee Mar 14 '12 at 17:39
-
Thanks for your fast reply.How to restart the service through Broadcast receiver.? – Sreenath Reddy Mar 14 '12 at 17:41
-
http://stackoverflow.com/questions/3552927/is-it-a-good-idea-to-have-a-service-just-to-register-for-intents – Arindam Mukherjee Mar 14 '12 at 17:58
-
@SreenathReddy: return START_STICKY or START_REDELIVER_INTENT from onStartCommand() method in your service.. – ρяσѕρєя K Mar 14 '12 at 18:00
-
What do you mean saying 'I force close the activity'? Closing activity normally won't stop any services they are stopped by using either stopService from activity or stopSelf from the service itself. – Alexander Kulyakhtin Mar 14 '12 at 18:02
-
@imrankhan:Thanks for your reply.But i have already included that in my code. check this code public int onStartCommand(Intent intent, int flags, int startId) { handleFunction(); System.out.println("IN onStartCommand......."); // We want this service to continue running until it is explicitly // stopped, so return sticky. return START_STICKY; } – Sreenath Reddy Mar 15 '12 at 05:37
2 Answers
1
You must implement the onStartCommand()
method by returning the START_STICKY
or START_REDILIVER_INTENT
, but it's your responsibility to stop the service by calling stopService()
or stopSelf()
method.

ZygD
- 22,092
- 39
- 79
- 102

Balvir Jha
- 47
- 2
0
You should return START_STICKY from onStartCommand in your service. This will make sure Android restarts your service after being killed. See the link bellow.
http://developer.android.com/reference/android/app/Service.html#START_STICKY
This will not deliver again the original intent. If you also need that return START_REDELIVER_INTENT.
These are also explained in the Android blog.
http://android-developers.blogspot.com.au/2010/02/service-api-changes-starting-with.html

azertiti
- 3,150
- 17
- 19