In Android, all programs are actually exited.

Source: Internet
Author: User

Http://www.cnblogs.com/jauntlin/archive/2011/05/28/2060712.html

Sometimes our software interface has an exit function and cannot use finish () in the activity. Because sometimes your application has multiple activities, I searched for how to completely exit the application on the Internet, there are three methods:

1. Use the killbackgroundprocesses method of activitymanager. This method also requires application permissions, which are not used in all SDK versions. The related code is as follows:

Activitymanager manager = (activitymanager) Context. getsystemservice (context. activity_service );
Manager. killbackgroundprocesses ("package ");

2 kill the process:

Use Android. OS. process. killprocess (Android. OS. process. mypid (); or system. Exit (0 );

3. Use the restartpackage method of activitymanager:

Manager. restartpackage ("package ");

I don't know why, but I was not successful. Later I used the broadcast mechanism. If a friend of mine is the same as me, the above methods would not work. I tried the broadcast mechanism and didn't need any permissions. Practices:

1. Write a parent class to inherit activity. Other activities inherit this parent class. The two important methods are as follows:

   private BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
 
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        finish();
    }
     
};
 
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction("ExitApp");
    this.registerReceiver(broadcastReceiver, filter);
}

Of course, you can also write the above code for each activity.

1. Add the following method to the activity to exit:

// Send a broadcast notification to close all forms
   public void close()
{
    Intent intent = new Intent();
    intent.setAction("ExitApp");
    this.sendBroadcast(intent);
    super.finish();
}

You can call this operation to exit.

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.