0

In android application if I save the intent of the service that is running. How can I restart the service? I want it to run onStartCommand function again.

Thanks

ida
  • 1,011
  • 1
  • 9
  • 17

1 Answers1

1

Make onStartCommand return START_STICKY .

public int onStartCommand(Intent intent, int flags, int startId){
   super.onStartCommand(intent, flags, startId);
   return  START_STICKY; 
}

Documentation link: http://developer.android.com/reference/android/app/Service.html

Pavlos
  • 2,183
  • 2
  • 20
  • 27
  • I have the code for starting the service in one place and in the other service which is meant for scheduling I stop and start the service in order to have the OnStartCommand called. Is that a problem? – ida Sep 25 '14 at 14:52
  • "Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance" From the official documentation! But i think this is not exactly what you want! Your question was a bit confusing – Pavlos Sep 25 '14 at 14:54
  • This return needed if service was killed. @ida, why you want to start exactly onStartCommand? And what problem in just what you do now: stopService(); startService(); – user3439968 Sep 25 '14 at 14:59
  • Yes It is not exactly what I want. I need to stop services in some points and start them again when necessary. How can I tell the OS that it is out of running state? And which part of my question is confusing for you? – ida Sep 25 '14 at 15:02
  • @user3439968 I need to read some parameter from the database at the start point of my service. So I need to start it from the beginning. And I need to put some parameters for the intent. So the thing that I am doing now for that is stopService(intent), intent.putExtra(...), StartService(intent). – ida Sep 25 '14 at 15:05
  • Then you need to rephrase your question and check the documentation for the return value that suits you best! Check the edit! – Pavlos Sep 25 '14 at 15:05
  • @ida, Pavlos answer is not what you need. I don't see anything bad in what you do now. Another way is use static variables to set new data if you services running in one process. Or you can use BroadcastRecievers if you don't want restart service. – user3439968 Sep 25 '14 at 15:21
  • I really dont get you @user3439968, what did i answer wrongly? – Pavlos Sep 25 '14 at 15:26
  • @Pavlos, it's because ida question not clear. I think he need just set new data to running service. Flags like START_STICKY needed if service was killed. BTW, if service need intent and if this service was killed, you need set START_REDELIVER_INTENT. Because START_STICKY don't redeliver intent after service was killed. – user3439968 Sep 25 '14 at 15:37
  • Yeah after clearing this out i pointed him to the official documentation! It wasn't clear at first! – Pavlos Sep 25 '14 at 15:40
  • @ida, use stopService(); startService(); What problem with this? – user3439968 Sep 25 '14 at 16:08
  • btw, you programmer, your code can be executing where you want. In OnStartCommand or where you like. Think maybe you need change logic execution of your program? I'm don't restarting my services. Just use broadcasts, configs, statics, singletons, etc. – user3439968 Sep 25 '14 at 16:13