Reputation: 3585
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?
Upvotes: 21
Views: 26366
Reputation: 31
In my case, I was using FusedLocationProviderClient in my service. To solve this Issue I called fusedLocationProviderClient.removeLocationUpdates(locatinCallback) before stopSelf.
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val action =intent?.action
if (action=="STOP_LOCATION_SERVICE"){
stopForeground(STOP_FOREGROUND_REMOVE)
fusedLocationProviderClient.removeLocationUpdates(locatinCallback)
stopSelf()
return START_NOT_STICKY
}
startForegroundService()
return START_STICKY
}
Upvotes: 0
Reputation: 91
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();
}
Upvotes: 0
Reputation: 1
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>
Upvotes: 0
Reputation: 11
if you both called startService and bindService.you should unbindService frist,otherwish the stopSelf will not work.
Upvotes: 1
Reputation: 4825
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)
Upvotes: -1
Reputation: 10641
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.
Upvotes: 13
Reputation: 890
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.
Upvotes: 12
Reputation: 37536
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();
}
Upvotes: 3
Reputation: 5907
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.
Upvotes: 3
Reputation: 16798
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.
Upvotes: 0
Reputation: 116372
If you use a foreground service, you should call:
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.
Upvotes: 34