[Android sharing] disabling running applications (essence Board)

Source: Internet
Author: User

From http://www.eoeandroid.com/forum.php? MoD = viewthread & tid = 158206.

Original Author Android-sylar


I have seen a lot of questions about application exit. I would like to give you a brief summary here today. I hope you will forgive me for saying something wrong.

Method 1: system. exit (0) and Android. OS. process. killprocess (Android. OS. process. mypid (), I think many people have tried. When multiple activities are closed, these two methods do not work at all, because of course they are related to the stack management of the activity.

Method 2: restartpackage. 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 intent will also be sent. action_package_restarted. As we all know, this method is outdated and 2.2 does not work.

  1. Activitymanager manager = (activitymanager) getsystemservice (activity_service );
  2. Manager. restartpackage (getpackagename ());
  3. <Uses-Permission Android: Name = "android. Permission. restart_packages"/>

Copy code


Method 3: replace restartpackage after 2.2. But Google only gives an empty shell, and this method still does not work.

  1. Activitymanager. killbackgroundprocesses (packagename );
  2. <Uses-Permission Android: Name = "android. Permission. kill_background_processes"/>

Copy code

Method 4: Create an activityinstance Singleton mode to uniformly manage the exit of the activity. This is a very bad method, because it is indeed useful, but it is a waste of writing it.

The specific method is to write an activityinstance singleton and call its activityinstance when each activity starts. addactivity (this); method. When exiting the application, activityinstance is called. exit (); method.

  1. Public void exit (){
  2. For (activity at: activitylist ){
  3. At. Finish ();
  4. }
  5. System. Exit (0 );
  6. }

Copy code

Method 5: Send the broadcast and exit. Send a specific broadcast when the application needs to end. Each activity is closed after receiving the broadcast. If you don't have the trouble, you can do this. In my opinion, there is no better way.
Method 6: Exit recursively. Use startactivityforresult when opening a new activity, add a flag, process it in onactivityresult, and disable it recursively.
Method 7: Jump back to home when exiting, a false exit method, but can exit. This method only returns to the home page and falsely exits its own applications.

  1. Intent startmain = new intent (intent. action_main );
  2. Startmain. addcategory (intent. category_home );
  3. Startmain. setflags (intent. flag_activity_new_task );
  4. Startactivity (startmain );
  5. System. Exit (0 );

Copy code

Method 8: activitymanager. forcestoppackage (packagename); check whether forcestop is quite familiar. In our system settings, closing the application is called. This method can completely close the application with only one package name.
However, this method is hidden by the system. We cannot find this method in activitymanager when writing an application. This involves calling the system to hide the API. The following method is obtained through the reflection mechanism.

  1. Activitymanager SD = (activitymanager) This. getsystemservice (activity_service );
  2. Method method = Class. forname ("android. App. activitymanager ")
  3. . Getmethod ("forcestoppackage", String. Class );

Copy code

Supplement: in fact, Android has its own memory management mechanism and does not need to completely exit its own application. When a user presses the return key, the user can return to home or other related interfaces. You can quit falsely without special requirements.

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.