Android termination process method

Source: Internet
Author: User


Android ends the process and closes the program. After several days of research, we found that android ends the process.

The following class is used:

Void Android. App. activitymanager. restartpackage (string packagename)

Public void restartpackage (String packagename)

Since: API level 3

Have the system perform a force stop
Everything associated with the given application package. All processes that
Share its uid will be killed, all services it has running stopped, all
Activities removed, etc. In addition, a action_package_restarted broadcast will be sent, so that any of its
Registered Alarms can be stopped, ications removed, etc.

You must hold the permission restart_packages to be able to call this method.

Parameters

Packagename

The name of the package to be stopped.

 

 

Use the specific source code of this class

JavaCode 

Final activitymanager AM = (activitymanager) getsystemservice (context. activity_service); <br/> am. restartpackage (getpackagename ()); 

Add uses-Permission

 

 

XMLCode 

<Uses-Permission Android: Name = "android. Permission. restart_packages"> </uses-Permission>

 

 

The android. OS. process. killprocess (PID) can only terminate the process of the program and cannot terminate other processes.

 

Public static final void killprocess (int pid)

Kill the process with the given PID. Note that, though
This API allows us to request to kill any process based on its PID, the kernel
Will still impose standard restrictions on which PIDs you are actually able
Kill. Typically this means only the process running the caller's
Packages/application and any additional processes created by that app; packages
Sharing a common uid will also be able to kill each other's processes.

 


Public void finish ()

Call this when your activity is done and shocould be
Closed. The activityresult is propagated back to whoever launched you
Onactivityresult ().

This is the method to end the current activity.

 

 

The restartpackage () method cannot be used after android2.2, but the killbackgroundprocesses () method should be used.

Manager. killbackgroundprocesses (getpackagename ());

 

Activitymanager manager = (activitymanager) getsystemservice (activity_service); <br/> manager. killbackgroundprocesses (getpackagename ()); 

Add permission

// Add a permission statement to the XML file <br/> <uses-Permission Android: Name = "android. Permission. kill_background_processes"/> 

 


 

In additionIn the future, if the service is in the ondestroyWith startUse kill backgroudprocessYou cannot end yourself.

 

There is also a newly discovered method that uses reflection to call forcestoppackage to end the process

 

The Code is as follows:

 

Method forcestoppackage = aM. getclass (). getdeclaredmethod ("forcestoppackage", String. class); <br/> forcestoppackage. setaccessible (true); <br/> forcestoppackage. invoke (AM, yourpkgname ); 

 

Shareduid must be added to manifest.

Android: shareduserid = "android. uid. System" 

 

Add Permissions

<Uses-Permission Android: Name = "android. Permission. force_stop_packages"> </uses-Permission> <br/> 

 

And the system platform is used.Signature


Because you need to use the force_stop_packages permission, this permission only grants the system signature-level program

 

You can force stop a specified program.

 

Another method is to use the kill-9 command of Linux.

 

 

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.