Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StopSelf does not stop my service

Tags:

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?

like image 453
Dean Blakely Avatar asked Dec 04 '12 23:12

Dean Blakely


1 Answers

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.

like image 136
android developer Avatar answered Sep 28 '22 04:09

android developer