Three ways to completely exit Android programs

Source: Internet
Author: User

Author: Android Development Network original time: 2010-06-10
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. Dalvik VM Local Method Android.os.Process.killProcess (Android.os.Process.myPid ())//Get PID, currently get own only that API, otherwise from/proc Your own enumeration of other processes, but to illustrate, the end of other processes do not necessarily have permissions, otherwise it is a mess. System.exit (0); Normal Java, C # Standard Exit method, with a return value of 0 representing a graceful exit of 2. The Task Manager method first to explain that the method runs on Android 1.5 API level is more than 3 can, at the same time need 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. Based on 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 a flag directly to the intent when opening the b window in the A window intent.flag_activity_ Clear_top, so opening B will erase all activity in that process space. In the a window, use the following code to call 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); 4. The Killbackgroundprocesses method adds an API to Android 2.2 to help us kill the background process, but Android123 again emphasizes that its call API level is at least 8, Killbackgroundprocesses is a method of the Android.app.ActivityManager class 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 call, the SDK has been marked as deprecated, but the principle is the same. But in the past, Google's naming method is really not very reasonable, the role of restartpackage it is difficult for us to associate with the end process. The method's prototype public void Killbackgroundprocesses (String packagename) has only one parameter for the package Name, which is easier to use. Activitymanager am = (activitymanager) getsystemservice (Activity_service); Am.killbackgroundprocesses ("CN.COM.ANDROID123.CWJ"); The API level is at least 8 to use 5. Dalvik VMS First we can make a null pointer exception, For example, the Textview.settext method executes an int, because the SetText method overloads the resource int content of R.STRING.XXX, but we do not declare this resource, just write the string int value, will produce an exception, then the system Dalvik VMS will directly shut down your process, which is very effective, CWJ usually not published to everyone. 6. Thread.uncaughtexceptionhandler But some netizens said, there is force Close dialog box how to do, In fact, by rewriting the Android application's application base class to implement its own Thread.uncaughtexceptionhandler interface Uncaughtexception method is to avoid the presence of FC Windows, the user feels directly exited the sameHttp://bbs.droidstouch.com/thread-1074-1-2.html

Three ways to completely exit Android programs

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.