Android program exits completely

Source: Internet
Author: User

 

There are several activities. One requirement is to click the back key in activitya to exit the system, instead of jumping to the previous activity. The first thing that comes to mind is to clear the activitya stack and use intent. setflags (intent. flag_activity_clear_top); but the activitya does not exist at the bottom of the stack, so only the activity on the activitya in the stack is cleared, but the activity under activitya in the stack is returned after the rollback. Then we thought of an event that intercepts the back button, using system. exit (0) and Android. OS. process. killprocess (Android. OS. process. mypid () to exit the program, or not. The current activitya is killed, but it will still be taken over by activitymanager and returned to the previous activity. In addition, if the previous activity requires some passing parameters, the program will throw an exception continuously. Finally, a feasible method is found: Use activitymanager to completely exit the program activitymanager manager = (activitymanager) getsystemservice (activity_service); manager. restartpackage (getpackagename (); permission <uses-Permission Android: Name = "android. permission. restart_packages "/> This method terminates all processes associated with this package, all processes that share the same uid are killed, and all activities are removed and all services created are stopped, A broadcast will also be sent, causing all registered alarms to be stopped, and notifications to be removed. The restartpackage method was invalid when I brushed the ROM of 2.2 in the past few days. I searched the internet and found that an API was added to Android 2.2 to help us kill background processes, however, 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. The prototype public void killbackgroundprocesses (string packagename) of this method has only one parameter: package name.

Single.

Activitymanager AM = (activitymanager) getsystemservice (activity_service );

 

Am. killbackgroundprocesses ("cn.com. android123.cwj"); // The API level must be at least 8.

 

Therefore, it is best to add a code to judge whether the ROM before 2.2 will use killbackgroundprocesse after restartpackage.

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.