The Android program has a lot of activity, such as the main window a, calling child window B, and child window B calling the child window C,back returns child window B, how to close the entire Android application in B? The following cloud Habitat Community Small series to introduce the Android development of the exit program of several methods.
1, Finish () method
Finish is the activity of the class, only for activities, when the call finish (), just push the event to the background, and did not immediately release the memory, the active resources are not cleaned, the call finish () method executes Activity.ondestroy ( ) method to end the activity lifecycle
In the development of Android applications, often by pressing the return key (that is, KeyCode ==keyevent.keycode_back) can close the program, in fact, in most cases the application is still running in the task, in fact, this is not the result we want.
2, Dalvik VM Local method
Android.os.Process.killProcess (Android.os.Process.myPid ());
Get the PID, you currently have only the API, or from/proc own enumeration of other processes, but to note that the end of other processes do not necessarily have permissions, or chaos.
System.exit (0);
Normal Java, C #, the standard exit method, the return value of 0 represents a graceful exit, can be written in the Ondestory method.
We can do this, when the user clicks the Custom exit button or returns the key (needs to capture the action), we forcibly exits the application in the OnDestroy (), or kills the process directly, the concrete operation code is as follows:
Capture keyboard Action
@Override public
boolean onKeyDown (int keycode, keyevent event) {
//when the return button on the keyboard is pressed, the Exit dialog box is given
( KeyCode = = keyevent.keycode_back) {
new Alertdialog.builder (This)
. SetIcon (r.drawable.services)
. Settitle (r.string.prompt)
. Setmessage (R.string.quit_desc)
. Setnegativebutton (R.string.cancel, new Dialoginterface.onclicklistener () {
@Override public
void OnClick (dialoginterface dialog, int which) {
})
. Setpositivebutton (R.string.confirm, New Dialoginterface.onclicklistener () {public
void OnClick ( Dialoginterface dialog, int whichbutton) {
finish ();/
}). Show ();
return true;
} else{return
Super.onkeydown (KeyCode, event);
@Override
protected void OnDestroy () {
Super.ondestroy ();
System.exit (0);//Direct End Program
//or the following way
//android.os.process.killprocess (Android.os.Process.myPid ());
3. Task Manager method
First of all to show that this method runs in the Android 1.5 API level of more than 3 to be able to, at the same time need permissions Android.permission.RESTART_PACKAGES, we directly end their package can.
Directly using the Restartpackage method of the Activitymanager class, the parameter is package name, which is passed through Getsystemservice (context.activity_service) To instantiate the Activitymanager object, which is provided by the system but needs to display declarative permissions, so it needs to be considered in use.
The emphasis is on this type of approach:
Activitymanager manager = (Activitymanager) getsystemservice (activity_service);
Manager.restartpackage (Getpackagename ());
Need to declare permissions
This method terminates everything associated with the package, and all processes sharing the same UID are killed and all activity is removed
All created services will stop and a broadcast intent.action_package_restarted will cause all registered alarms to be stopped and notifications removed.
But a new API in Android 2.2 can help us kill background processes (Android2.3 again emphasizes that the API level of the call is minimal to 8) Killbackgroundprocesses is a Android.app.ActivityManager class method that must be added to the Androidmanifest.xml file when used Kill_background_ Processes this permission. Although this class also provides an API level of 3 for the Restartpackage (String PackageName) method invocation, the SDK has been marked as deprecated, but they are all the same. It's just that the way Google's naming is not very reasonable in the past, it's hard for us to associate Restartpackage with the end process.
The prototype of the method:
The public void killbackgroundprocesses (String packagename) has only one argument for package Name and is simpler to use.
Activitymanager am = (activitymanager) getsystemservice (Activity_service);
Am.killbackgroundprocesses ("CN.COM.ANDROID123.CWJ");/API level is at least 8 to use
You must include kill_background_processes this permission in the Androidmanifest.xml file.
Therefore, it is best to use this method to add a judge if the ROM before 2.2 with Restartpackage, after the use of killbackgroundprocesses.
Activitymanager am = (activitymanager) getsystemservice (activity_service);
if (Android.os.Build.VERSION.SDK_INT < 8) {
am.restartpackage (Getpackagename ());
} else{
am.killbackgroundprocesses (Getpackagename ());
Reference:
4. According to the statement cycle of the activity
We know that the Android window class provides a history stack, and we can do this by using the stack principle, where we add the flag intent.flag_activity_clear_top directly to the intent when we open the B window in window A. By opening B, all activity in the process space will be cleared.
Use the following code in window A to invoke the b window
Intent Intent = new Intent ();
Intent.setclass (Android123.this, cwj.class);
Intent.setflags (Intent.flag_activity_clear_top); Note that the flag setting of the bank
StartActivity (intent);
Next in the b window you need to exit the direct use of the finish method to exit all.
The above is a small set for everyone to share the Android development Exit program Summary, I hope to help!