Stop a sticky service programmatically - android

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?

Related

android : stop a sticky service

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.

Does a Service in Android stop when the calling Activity/Fragment changes orientation?

I am looking at the doc but it doesn't really explain this anywhere. I did find this:
The service will at this point continue running until Context.stopService()
or stopSelf() is called.
What this tells me is the Service will continue running, but that is assuming when orientation change occurs that the Activity/Fragment does not automatically stop services it has started. I also found this:
Using startService() overrides the default service lifetime that is managed by
bindService(Intent, ServiceConnection, int): it requires the service to remain
running until stopService(Intent) is called, regardless of whether any
clients are connected to it.
Can I assume that the Service will actually not stop until I explicitly call stopService?
for the title, answer is no.
A service doesn't stops when the calling activity/fragment changes orientation. It continues running unless or untill it is stopped explicitly
for the description, the answer is yes.
you definitely need to call either of the functions stopService() or stopSelf() to stop the service.
Yes.You have to stop it using stopService() method or you can use stopSelf() to stop when the the service is no longer needed.it will automatically stop the service.

StopSelf does not stop my service

Googling around, I have seen this problem posted many times, some of the posts on this forum over that last few years. I have never seen anyone get a straight answer.
I have a foreground service that tries to stop itself after running for 2 hours by doing a this.StopSelf(). I've tried it when it's bound and when it's unbound. On the AVD and on a device.
It simply does not work. Is this a bug in Android? (running 2.3.3).
How can a service stop itself?
If you use a foreground service, you should call:
stopForeground(true);
Just like you start it with startForeground.
stopSelf is meant to be used for normal services. a foreground service is special... :)
By the way, in case your app should work on older devices, you should check the compatibility code offered here.
The only way I have found to stop a service with certainty is to use:
android.os.Process.killProcess(android.os.Process.myPid());
This behavior can be caused by using worker threads that have not finished when the stopSelf is called. If you are using worker threads, be careful how you employ this - you may leak memory if you don't clean up before executing the line.
I met a similar problem.Through my search found that: In other activity's onCreate method I bind the service,but not unbind it onDestory method.After fix that,when I call stopSelf then the service's onDestory method is call. I hope it can help you.
At same time,I find base from Android gover document.
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().
I had this problem and searching I found this thread. My app's problem is that when service calls stopSelf(), the foreground activity that binds the service is still running. Thne stopSelf is called but the service is not destroyed as the foreground activity is still bound. When I leave the activity by pressing back or going to home screen, then the service is destroyed. In short, stopSelf won't work when a foreground activity is bound.
I encounter this issue of selfStop() that not working too.
The main idea to understand is that the Service WILL NOT stop,
if a running loop is still running (while(mDoWhile) for example).
Or any other issue that need to be destroyed / unregistered first.
So to make the stopSelf() or stopService(intent) works,
implement onDestroy() within your service:
#Override
public void onDestroy()
{
// Unregistered or disconnect what you need to
// For example: mGoogleApiClient.disconnect();
// Or break the while loop condition
// mDoWhile = false;
super.onDestroy();
}
if you both called startService and bindService.you should unbindService frist,otherwish the stopSelf will not work.
I was facing same problem too.
As we all know that Service runs on App's UI Thread. So, until you've Thread that is running in background in your service and you are calling stopSelf() then it won't work. For that you have to stop that Thread first and then execute stopSelf().
After stoping all background Thread from your Service stopSelf() method will definitely work.
The only way i had to kill the foreground service was to set stopwithtask=true in the manifest
<service
android:name="my.package.MyForegroundService"
android:foregroundServiceType="location"
android:stopWithTask="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
</service>
In my case of foreground service, it was necessary to remove location updates, unregister sensors, etc.
Add the following code to OnStartCommand when you want to kill the service:
private void selfTerminate() {
stopForeground(true);
unregisterBarometer();
if (locCallBack != null) {
getFusedLocationProviderClient(this).removeLocationUpdates(locCallBack);
}
m_tripState.currentActivity.clock.stopClock();
stopSelf();
}
stopSelf might not destroy the service if the conditions are not favorable, but you can stopService with the application context. But make sure the service doesn't restart before calling stopService inside service.
stopService(applicationContext, MyService::class.java)

When should a Service be stopped

My android application starts a service in the onCreate() callback of a class that extends Application. The service performs some background tasks that are relevant to the user only while the application is running. For that reason I would like to close the service when the application's last activity is closed. I've tried to perform closing the service in the callback onTerminate() , but it never gets called . So what would be the best place where a service should be closed ?
Thanks !
An Android service, once started, will continue running until the Context.stopService() or stopSelf() is called.
There are various hooks you can use to stop the service using Context.stopService (the service itself, or an onDestroy()/onPause callback in one of the activities, or a button click).
It's true that Android does some resource management itself, but it can take a long time before Android decides to terminate your services. And a service that's running but not doing anything just consumes resources on the phone that other apps might need.
In your case, the onPause method of your last activity would be a good that will get called, and as such is the correct place to stop the service.
The onPause() callback will be made when your activity is paused for any reason, and you know that when this happens your app will not be visible again until onResume() is called. If your service has a reason to run in the use case that your activity might be started again soon, you should add an entry to your service that onPause() calls, to set a delayed service termination. In onResume() you can cancel that delayed termination through another entry.

calling onDestroy in android shutdown

If I have a service running in my Android phone and I shutdown, will the service call onDestroy? I want the service to perform a task just before the phone shuts down. Although the Google Droid guide says not to put such code in onDestroy, I do not know what else can be done.
If startService() is called to create your Service you should call stopService(), then onDestroy() will be called. If you use bindService() with BIND_AUTO_CREATE flag, than unbindService() caused onDestroy() automatically. What code they say shouldn't be put there? If it uses this Service of cause onDestroy() is not a good idea, if it's something like notification it's ok to put it there.

Categories

Resources