Android ends other processes in the system

Source: Internet
Author: User

Although Google agrees with the theory that applications do not completely exit, it believes that android is fully capable of automatically releasing some memory when the memory is insufficient, but the majority of users cannot fully support this theory, the task manager was created to end the process.

Here I will write down the process termination method, which may be flawed ~

View plain
  1. Int start = 0, end = 0;
  2. <Span style = "white-space: pre"> </span> activitymanager = (activitymanager) Context. getsystemservice (context. activity_service );
  3. List <activitymanager. runningappprocessinfo> runningappprocesses = activitymanager. getrunningappprocesses ();
  4. If (runningappprocesses! = NULL ){
  5. <Span style = "white-space: pre"> </span> Start = runningappprocesses. Size ();
  6. }
  7. For (activitymanager. runningappprocessinfo: runningappprocesses ){
  8. Activitymanager. restartpackage (runningappprocessinfo. processname );
  9. }
  10. Runningappprocesses = activitymanager. getrunningappprocesses ();
  11. If (runningappprocesses! = NULL ){
  12. <Span style = "white-space: pre"> </span> end = runningappprocesses. Size ();
  13. }
  14. Toast. maketext (context, "successfully ended" + (START-end) + "program", Toast. length_short). Show ();

It's easy to get runningappprocesses first, and then close it using the restartpackage method.

Note the following two points:

1. to be compatible with more Android versions, you need to use the restartpackage method. Do not use the kill_background_processes method of 2.2, because the previous version does not have this method, the killbackgroundprocess method after 2.2 is only encapsulation of restartpackage. It has been said before that the process can be ended by using this method in 2.2.

2. Even if you are using the restartpackage method, add the Android. Permission. kill_background_processes permission to the Declaration, that is, add

View plain
  1. <Uses-Permission Android: Name = "android. Permission. kill_background_processes"/>
  2. <Uses-Permission Android: Name = "android. Permission. restart_packages"/>

These two permissions are required because the killbackgroundprocess method is automatically called in 2.2.


There is a problem here. The process obtained by getrunningappprocesses is not the package name of each running program, the method for getting the package name using processname is not found (one program can have multiple processes and one process can run multiple programs). I checked the structure of processname, such as COM. naivesoft. such as test, there are also COM. naivesoft. test: remote, the package name can be obtained through Character Parsing. It is not clear whether this method is the best, but it is also possible to end processname directly using restartpackage, so we did this for the time being. Please kindly advise on how to be the best.

Although Google agrees with the theory that applications do not completely exit, it believes that android is fully capable of automatically releasing some memory when the memory is insufficient, but the majority of users cannot fully support this theory, the task manager was created to end the process.

Here I will write down the process termination method, which may be flawed ~

View plain
  1. Int start = 0, end = 0;
  2. <Span style = "white-space: pre"> </span> activitymanager = (activitymanager) Context. getsystemservice (context. activity_service );
  3. List <activitymanager. runningappprocessinfo> runningappprocesses = activitymanager. getrunningappprocesses ();
  4. If (runningappprocesses! = NULL ){
  5. <Span style = "white-space: pre"> </span> Start = runningappprocesses. Size ();
  6. }
  7. For (activitymanager. runningappprocessinfo: runningappprocesses ){
  8. Activitymanager. restartpackage (runningappprocessinfo. processname );
  9. }
  10. Runningappprocesses = activitymanager. getrunningappprocesses ();
  11. If (runningappprocesses! = NULL ){
  12. <Span style = "white-space: pre"> </span> end = runningappprocesses. Size ();
  13. }
  14. Toast. maketext (context, "successfully ended" + (START-end) + "program", Toast. length_short). Show ();

It's easy to get runningappprocesses first, and then close it using the restartpackage method.

Note the following two points:

1. to be compatible with more Android versions, you need to use the restartpackage method. Do not use the kill_background_processes method of 2.2, because the previous version does not have this method, the killbackgroundprocess method after 2.2 is only encapsulation of restartpackage. It has been said before that the process can be ended by using this method in 2.2.

2. Even if you are using the restartpackage method, add the Android. Permission. kill_background_processes permission to the Declaration, that is, add

View plain
  1. <Uses-Permission Android: Name = "android. Permission. kill_background_processes"/>
  2. <Uses-Permission Android: Name = "android. Permission. restart_packages"/>

These two permissions are required because the killbackgroundprocess method is automatically called in 2.2.


There is a problem here. The process obtained by getrunningappprocesses is not the package name of each running program, the method for getting the package name using processname is not found (one program can have multiple processes and one process can run multiple programs). I checked the structure of processname, such as COM. naivesoft. such as test, there are also COM. naivesoft. test: remote, the package name can be obtained through Character Parsing. It is not clear whether this method is the best, but it is also possible to end processname directly using restartpackage, so we did this for the time being. Please kindly advise on how to be the best.

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.