I have an android app, which I would like to close if it is open in background by sending a notification. Is this possible. If yes, how?
Asked
Active
Viewed 127 times
0
-
1Technically, this is possible. However, I think some Android guidelines say you shouldn't explicitly close your app from within the code. See discussion on the topic [here](http://stackoverflow.com/q/2033914/3372061). – Dev-iL Oct 06 '16 at 12:24
3 Answers
1
Implementation of the complete FCM code to handle notifications can be found here. Doesn't make sense to copy everything here, given is simply following the official guide.
Then, inside your handler simply do a System.exit(0);

Xavier Rubio Jansana
- 6,388
- 1
- 27
- 50
-
-
Yes, the alternatives are: keep track of all the Activities and `.finish()` them, or move the app to background (I understand that this is not what you intend to do) with `moveTaskToBack(true);` – Xavier Rubio Jansana Oct 06 '16 at 15:01
-
What exactly do you mean the by closing the app? Close all the Activities? – Xavier Rubio Jansana Oct 06 '16 at 15:02
0
yes, check the notification string and if it's true the app close
if(stringFromFirebase.equals("close_app")) {
System.exit(0);
}

Daniel Juric
- 128
- 1
- 10
-
I tried that, but instead of closing the app, system.exit(0) is force stopping the app which I don't want.. – milan m Oct 06 '16 at 12:40
-
1To add; Never (and I really mean never) compare the value of a String with '=='. Always use `equals` – 0xDEADC0DE Oct 06 '16 at 12:58
-
1@milanm You could use `finish()` instead. That's the proper "Android way". – Markus Kauppinen Oct 06 '16 at 12:58
-
@milanm you want to close the background via remote if the app is in background? – Daniel Juric Oct 06 '16 at 14:00
0
try this, it will kill entire app process.
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);

Keyur Thumar
- 608
- 7
- 19