Exit the entire application:
Close the current Activity method one
Finish ();
Close the current interface method two
Android.os.Process.killProcess (Android.os.Process.myPid ());
Close the current interface method three
System.exit (0);
Close the current interface method four
This.ondestroy ();
1. Manufacturing throw exception causes the entire program to exit
2. Put all the activity into a list and then drop all the activity,finish when you need to exit
Import java.util.LinkedList;
Import java.util.List;
Import android.app.Activity;
Import android.app.Application;
/**
* A class used to end all background activity
* @author Administrator
*
*/
public class Sysapplication extends application {
Using lists to keep each activity is key
Private list<activity> mlist = new linkedlist<activity> ();
To implement a static object that is created every time you use the class without creating a new object
private static sysapplication instance;
Construction method
Private Sysapplication () {}
Instantiate once
Public synchronized static Sysapplication getinstance () {
if (null = = Instance) {
Instance = new Sysapplication ();
}
return instance;
}
Add Activity
public void addactivity (activity activity) {
Mlist.add (activity);
}
Close each activity in the list
public void exit () {
try {
for (Activity activity:mlist) {
if (activity! = NULL)
Activity.finish ();
}
} catch (Exception e) {
E.printstacktrace ();
} finally {
System.exit (0);
}
}
Kill process
public void Onlowmemory () {
Super.onlowmemory ();
System.GC ();
}
}
3. The exit function is accomplished by broadcasting, which is accomplished by registering a broadcast receiver with the activity at each activity creation (OnCreate) and sending the broadcast when exiting. The approximate code is as follows:
@Overrideprotected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Intentfilter filter = new Intentfilter (); Filter.addaction ("Finish"); Registerreceiver (Receiver, filter);} Private Broadcastreceiver Receiver = new Broadcastreceiver () { @Override public void OnReceive (context context, Intent Intent) { if ("Finish". Equals (Intent.getaction ())) { log.e ("#########", "I AM" + getlocalclassname () C11/>+ ", now finishing myself ..."); Finish ();}} ;
Android Exit Program Writing Summary