Android disables an application

Source: Internet
Author: User
When developing an Android Application, consider how to close the application. At first, the current activity is used to call finish (). However, this method is incorrect. In the past few days, when I read the code at the frameworks layer of Android, I found that the platform provided a method to close the program. The following methods are provided in the activitymanager class:
As follows:
/**
* Have the system perform a force stop of everything associated
* 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 {@ link intent # action_package_restarted}
* Broadcast will be sent, so that any of its registered Alarms can * be stopped, notifications removed, etc.
*
* You must hold the permission * {@ link Android. manifest. Permission # restart_packages} To be able
* Call this method.
*
* @ Param packagename the name of the package to be stopped.
*/
Public void restartpackage (string packagename ){
Try {
Activitymanagernative. getdefault (). restartpackage (packagename );
} Catch (RemoteException e ){}}
OK. You only need to obtain the activitymanager instance and call this method to close the application normally. The following is a code snippet for closing a program:
Private void exit2 (){
Activitymanager actmgr = (activitymanager) This. getsystemservice (activity_service );
Actmgr. restartpackage (getpackagename ());}
The above method can be called to completely close the program. Generally, the program cannot be completely closed only by calling finish. However, to call the system service, you must add the permission to the androidmanifest. xml file.
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.