Three methods for completely exiting the android Program

Source: Internet
Author: User
AndroidProgramThree methods of completely exiting: Android Development Network original time: 2010-06-10 Many users may find that their android program has a lot of activities, such as the main window a, calling subwindow B, and how to close the entire Android Application in B? ? Here, android123 provides three simple methods for implementation. First, you need to explain how to use finish () directly in B. Next, the mobile phone displays the main window a, so let's take a look at how Android development network is implemented. 1 Local method of the. Dalvik VM Android. OS. process. killprocess (Android. OS. process. mypid ())  //  Obtain the PID. Currently, only this API is available for obtaining the PID. Otherwise, enumerate other processes from/proc. However, you may not have the permission to end other processes, otherwise, it will be messy. System. Exit (0 ); //  Standard exit method of Java and C #. If the returned value is 0, the system Exits normally. 2. The task manager method must first demonstrate that this method runs on Android 1.5 The API level is 3 or above, and the permission android. permission. restart_packages: You can directly end your package by 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. This method is provided by the system, but the declared permissions need to be displayed. 3 . According to the activity declaration cycle, we know that the android window class provides a historical stack. We can use the stack principle to implement it skillfully, here we add the intent flag to intent when opening window B in window. flag_activity_clear_top. When B is enabled, all activities in the process space are cleared. Use the followingCodeCall intent of window B = New  Intent (); intent. setclass (android123.  This , Cwj. Class ); Intent. setflags (intent. flag_activity_clear_top );  //  Pay attention to the flag settings of this line.  Startactivity (intent ); 4 The. killbackgroundprocesses method is available in Android 2 . 2 added an API to help us kill background processes, but android123 once again stressed that the minimum API level it calls is 8, and killbackgroundprocesses is Android. app. activitymanager class method, which must be used in androidmanifest. add the kill_background_processes permission to the XML file. Although this class also provides the restartpackage (string packagename) method to call the API level of 3, but the SDK has been marked as deprecated, in fact, their principles are the same, however, in the past, the naming method of Google was indeed not very reasonable, and the role of restartpackage was hard to remind us of ending the process. Prototype of this method  Public   Void Killbackgroundprocesses (string packagename) has only one parameter: package name, which is easy to use. Activitymanager AM = (Activitymanager) getsystemservice (activity_service); am. killbackgroundprocesses ( "Cn.com. android123.cwj "); //  API level at least 8 5 . Dalvik VM first we can create a null pointer exception, such as textview. the settext method executes an int-type content, because the settext method overload R. string. INT-type content of a resource such as XXX, but we did not declare this resource. If we only write string as the int value, an exception will occur, at this time, the system Dalvik VM will directly shut down your process, which is very effective. cwj is generally not published to everyone. 6 . Thread. uncaughtexceptionhandler but some netizens have said what to do with the force close dialog box. In fact, we can rewrite the Android Application Base class to implement thread by ourselves. the uncaughtexceptionhandler interface's uncaughtexception method can avoid the FC window. The user feels that the same HTTP is exited directly: //  Bbs.droidstouch.com/thread-4874-1-2.html 

 

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.