Three ways to completely exit Android programs

Source: Internet
Author: User

Many users may find their Android program has a lot of activity, such as the main window A, called sub-window B, in b How to shut down the entire Android application? Here Android123 give you three kinds of relatively simple way to achieve.

First of all to explain the direct use of the finish in B (), and then the phone shows the main window A, so together to see how the Android Development network is implemented.

1. Local methods for Dalvik VMS

Android.os.Process.killProcess (Android.os.Process.myPid ())//Get PID, currently get its own also only the API, otherwise from/PROC in its own enumeration of other processes it, but to illustrate that,   Ending other processes does not necessarily have permissions, or it is a mess.   System.exit (0); Normal Java, C # Standard Exit method with a return value of 0 for graceful exit

2. Task Manager method

First of all to demonstrate that the method runs on Android 1.5 API level is more than 3 can, while the need for permission Android.permission.RESTART_PACKAGES, we directly end their package, Directly using the Activitymanager class's Restartpackage method, the parameter is package name, which is passed Getsystemservice (context.activity_service) To instantiate the Activitymanager object, which is provided by the system but needs to be displayed with declarative permissions, so it needs to be considered in a comprehensive way.

3. According to the activity's declaration cycle

We know that the Android window class provides a history stack, which we can skillfully implement using the stack principle, where we add the flag intent.flag_activity_clear_top directly to the intent when opening the b window in a window. When you turn on B, all activity for that process space will be cleared.

In the a window, use the following code to invoke the b window

Intent Intent = new Intent (); Intent.setclass (Android123.this, Cwj.class);  Intent.setflags (Intent.flag_activity_clear_top); Note The bank's flag setting startactivity (intent);

Often need to quit an Android application, if the application is a lot of activity, directly with the finish () can only quit the current activity, but not completely exit, although there are many ways to implement the online, I also give you a way to introduce a realization. For example, now that I have n activity, and every activity inside can use the menu key to exit the application, what should I do? Does every activity have a menu written in it? Of course not, we can define a general baseactivity, the activity of course inherits the most basic activity, and we implement the menu function in the baseactivity, realizes the program exit, The other activity in the program is then inherited to the baseactivity.

The code is as follows:

Package com.apkstory;

Import java.util.ArrayList;

Import android.app.Activity; Import Android.app.AlertDialog; Import Android.content.DialogInterface; Import Android.content.DialogInterface.OnClickListener; Import Android.os.Bundle; Import Android.util.Log; Import Android.view.Menu; Import Android.view.MenuItem;

public class Baseactivity extends Activity {

public static final String TAG = "baseactivity"; public static arraylist<activity> activitylist = new arraylist<activity> ();

@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Activitylist.add ( this); LOG.I (TAG, activitylist.tostring ()); }

@Override public boolean Oncreateoptionsmenu (Menu menu) {menu.add (0, 0, 0, "exit program"); return Super.oncreateoptionsmenu (men u); }

@Override public boolean onoptionsitemselected (MenuItem item) {if (item.getitemid () = = 0) {Exitapp ();} return Super.ono ptionsitemselected (item); }

@Override protected void OnDestroy () {Super.ondestroy (); Activitylist.remove (this); LOG.I (TAG, activitylist.tostring ()); }

/** * Completely quit the application */public void Exitapp () {Alertdialog.builder Builder = new Alertdialog.builder (this); Builder.settitle ( "Hint"); Builder.setmessage ("Are you sure you want to exit the program?"); Builder.setpositivebutton ("OK", new Onclicklistener () {

@Override public void OnClick (Dialoginterface dialog, int. which) {if (activitylist.size () &gt; 0) {for (Activity act Ivity:activitylist) {activity.finish ();} android.os.Process.killProcess (Android.os.Process.myPid ()); } } }); Builder.setnegativebutton ("Cancel", null); Builder.show (); }

}

Import java.util.LinkedList; Import java.util.List;

Import android.app.Activity; Import android.app.Application;

public class Exitapplication extends application {

Private list<activity> activitylist=new linkedlist<activity> ();

private static exitapplication instance;

Private Exitapplication () {}//Gets a unique exitapplication instance in singleton mode public static Exitapplication getinstance () {if (null =  = Instance) {instance = new exitapplication (); } return instance;

}//Add activity to the container public void addactivity (activity activity) {Activitylist.add (activity); }//Traverse all activity and finish

public void exit () {

for (Activity activity:activitylist) {activity.finish (); }

System.exit (0);

}} The following three classes indexactivity, Bactivity,cactivity is a simple example, respectively, is the indexactivity–>bactivity–>cactivity of the jump order. The Exitapplication.getinstance (). Addactivity (activity activity) method is called in the OnCreate () method in each activity class. When exiting an application in any of the activity interfaces, simply call the Exitapplication.getinstance (). Exit () method to completely exit the application in any activity.  Indexactivity class Source code: View Plaincopy to Clipboardprint?  Import android.app.Activity;  Import android.content.Intent;  Import Android.os.Bundle;  Import Android.view.View;  Import Android.view.View.OnClickListener; Import Android.widget.Button;

public class Indexactivity extends activity {/** Called when the activity is first created. */@Override public void O  Ncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);

Button next= (button) Findviewbyid (R.id.next_to_b); Next.setonclicklistener (Nextclick);

Button exit= (button) Findviewbyid (R.id.exit_main);  Exit.setonclicklistener (Exitclick); Exitapplication.getinstance (). addactivity (this);

}

Onclicklistener nextclick=new Onclicklistener () {

@Override public void OnClick (View v) {//TODO auto-generated method stub

Intent intent=new Intent (indexactivity.this,bactivity.class); StartActivity (Intent);

}  };

Onclicklistener exitclick=new Onclicklistener () {

@Override public void OnClick (View v) {//TODO auto-generated Method Stub exitapplication.getinstance (). exit ();  }  }; } bactivity Class Source code:

Import android.app.Activity;  Import android.content.Intent;  Import Android.os.Bundle;  Import Android.view.View;  Import Android.view.View.OnClickListener; Import Android.widget.Button;

public class Bactivity extends Activity {

@Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method stub super.oncreate (Savedi Nstancestate);

Setcontentview (r.layout.b);  Button next_to_c= (button) Findviewbyid (R.id.next_to_c); Next_to_c.setonclicklistener (Next_to_cclick);

Button exit_b= (button) Findviewbyid (r.id.exit_b);  Exit_b.setonclicklistener (Exitclick); Exitapplication.getinstance (). addactivity (this);

}

Onclicklistener next_to_cclick=new Onclicklistener () {

@Override public void OnClick (View v) {//TODO auto-generated method stub

Intent intent=new Intent (bactivity.this,cactivity.class); StartActivity (Intent);

}  };

Onclicklistener exitclick=new Onclicklistener () {

@Override public void OnClick (View v) {//TODO auto-generated Method Stub exitapplication.getinstance (). exit ();  }  }; } cactivity Class Source code:

Import android.app.Activity;  Import Android.os.Bundle;  Import Android.view.View;  Import Android.view.View.OnClickListener; Import Android.widget.Button;

public class Cactivity extends activity{

@Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method stub super.oncreate (Savedi Nstancestate);

Setcontentview (R.LAYOUT.C);

Button exit_c= (button) Findviewbyid (R.id.exit_c);  Exit_c.setonclicklistener (Exitclick); Exitapplication.getinstance (). addactivity (this);

}

Onclicklistener exitclick=new Onclicklistener () {

@Override public void OnClick (View v) {//TODO auto-generated Method Stub exitapplication.getinstance (). exit ();  If only one of the following methods is called, the application//android.os.process.killprocess (Android.os.Process.myPid ()) will not be completely exited;  System.exit (0); }  };

}

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.