I am looking for a solution to kill a service only when my app itself is paused or closed. Please note, I am not referring to one activity, I am looking for when any activity within the app is in the foreground while it is paused or killed. I have a lot of activities, so I don't want to extend them all from a BaseActivity. Is there any other way to do this ?
Asked
Active
Viewed 919 times
0
-
1check this https://github.com/friendlyrobotnyc/TinyDancer/blob/master/library/src/main/java/com/codemonkeylabs/fpslibrary/Foreground.java – aptyp Jul 18 '16 at 05:58
-
This makes no sense. Please explain what your `Service` is doing and why you need such strange behaviour. – David Wasser Jul 18 '16 at 08:39
-
1@DavidWasser - Strange behavior? I don't think you have much experience with Android if you think an activity controlling the lifecycle of a service is strange behavior. – Sofia Clover Jul 18 '16 at 23:45
-
@DavidWasser - what part of this question is strange ? – Bamerza Jul 18 '16 at 23:46
-
Your question is unclear. Apps are not "paused" or "closed". You cannot "kill" a `Service`. You have at your disposal `startService()`, `bindService()`, `stopService()`. You can send data to your `Service` in an `Intent`. Please use clearer language. What exactly is it that you want to do? – David Wasser Jul 19 '16 at 10:16
-
@SofiaClover my 43.000 reputation points are all from answering Android questions, so you must be right: I don't have much experience with Android. – David Wasser Jul 19 '16 at 10:17
2 Answers
0
Set an uncaught exception handler for your main thread in onCreate
of your Application
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// Kill your service here
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
});

hadilq
- 1,023
- 11
- 25
-
Is pausing/closing an app trigger an uncaught exception ? Please explain why you think the Thread exception occurs by doing so? – Sofia Clover Jul 18 '16 at 06:41
-
There is no "close" for Android apps!! So I expect it would be a crash. – hadilq Jul 18 '16 at 07:32
0
the system will try to re-create your service after our App/service is killed
the system will not try to re-create your service after our App/service is killed
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want to stop this service, when our Application is
// not running (foreground/background), so return non-sticky.
return START_NOT_STICKY;
}
Hope this would help you!

Karthi R
- 1,338
- 10
- 25
-
I am not worried about restarting the service. As stated, I need to kill the service when any activity in my app is paused or closed. – Sofia Clover Jul 18 '16 at 06:38
-
If you start your service with **START_NOT_STICKY** flag; System will take care of killing your service when your Application is not active in foreground/background. – Karthi R Jul 18 '16 at 06:59
-
The definition of both explicitly states they are invoked only when the "service's process is killed". If the app goes onPause, the process is not killed. – Sofia Clover Jul 18 '16 at 07:14