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() |
public void onReceive(Context arg0, Intent arg1) { |
// TODO Auto-generated method stub |
protected void onResume() { |
// TODO Auto-generated method stub |
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 |
Intent intent = new Intent(); |
intent.setAction( "ExitApp" ); |
this .sendBroadcast(intent); |
You can call this operation to exit.