Android Exit Program Writing Summary

Source: Internet
Author: User
Tags throw exception

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.