I am developping a wear app.
I start a sticky service in the wear when I receive a dedicated event.
This service returns START_STICKY in onStartCommand.
My question is : how can I stop definitively this service?
I need it to be restarted if Android stops it because of low memory, but I need to be able to stop it definitively by code.
I tried to stop it using stopService(Intent i) or stopSelf() but the service gets recreated each time.
Thank you for your help.
jn.
Call stopService(intentOfYourService) at onPause() or onDestroy() and give the Intent you created at onCreate() (or elsewhere) to the parameteres.
Related
I want to do this acts with my Service :
1)start App
3)start a Service by App
4)update my UI by Service
5)close App but the Service keep work
6)sart App again
7)connect to Service (not run new Service)
I want in my service public variable during living Service do not change.
If you return START_STICKY from onStartCommand() then your Service will run unstil it is explicitly stopped. You can then decide how you want to stop your Service, either by something calling stopService() or by the Service calling stopSelf() when it no longer wants to run.
Please also note that Android can kill your Service pretty much whenever it wants to. If you have returned START_STICKY then Android will restart your Service after it kills it, but there is nothing you can do to ensure that Android will not kill your Service.
I need a sticky service so that in case it is destroyed by the system it gets recreated, but I need it to stop working once the user logs out of the app.
Service.stopSelf() or Context.stopService(Intent) do not seem to work....
what should I do then? There must be a way...
Call stopService(intent_of_your_service) at onPause() or onDestroy().
Here, intent_of_your_service is the Intent you created at onCreate() or elsewhere.
Moreover, I am sure that stopSelf() works fine (i.e., it stops the service) even if it is a sticky service. From where did you call it?
I have a service in my application but this service stop working after time and i want to keep the service keep working without stopping and for that i try to start the same service from onDestroy method when it dieing but its not work even the written log not appear ,so it is possible to call the service from itself and if it not may i create another service who do the same work and they call each other when one of them stopped, so when in onDestory i cant call it so how to call it?, wish that someone can help me with this situation thnx on advance
You should not try to start the service again yourself. Better return START_STICKY or START_REDELIVER_INTENT from onStartCommand() and the system will do it for you when it has enough resources.
See the following link where there is also an example.
http://developer.android.com/guide/components/services.html
From my little android knowledge I understand that android OS can kill my service under extreme memory conditions.
I have created a service that returns START_STICKY. The service is meant to run in background.
If android is about to kill my service, will it call onDestroy ?
And when it restarts it would it call onCreate ?
See here, the dev guide. http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
onCreate() is only called when the process starts, which can either be the first time the service is running, or if it was killed on restarted, essentially this is called whenever it starts.
onStartCommand() is called whenever a client calls startService().
When a service is destroyed / completely stopped, Android is supposed to call onDestroy() on that service. I think it's possible for that to not happen (e.g. process is killed not through Android system). In the case of a bound service, this is when there are not more active client binders.
Edit: onCreate() Service starts; onStartCommand()someone uses service; onDestroy()Service is killed / stopped.
If someone calls Context.startService() then the system will retrieve
the service (creating it and calling its onCreate() method if needed)
and then call its onStartCommand(Intent, int, int) method with the
arguments supplied by the client
...
A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these situations
hold, the service's onDestroy() method is called and the service is
effectively terminated. All cleanup (stopping threads, unregistering
receivers) should be complete upon returning from onDestroy().
http://developer.android.com/reference/android/app/Service.html
EDIT: Quick answer. Yes to both questions
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.
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.
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