Android program exit method and how to listen for and process the user's press back key

Source: Internet
Author: User

It is troublesome to exit the program in Android, especially in multiple Activity programs. Before Android 2.2, you can exit the program using the following code:

Java code

 

 

[Java]
ActivityManager am = (ActivityManager) getSystemService (Context. ACTIVITY_SERVICE );
Am. restartPackage (getPackageName ());

ActivityManager am = (ActivityManager) getSystemService (Context. ACTIVITY_SERVICE );
Am. restartPackage (getPackageName ());


This method is the most convenient and simple way to exit the program, but it cannot be used after 2.2 and 2.2. If we want to exit the program, there are four methods:

Using the content view Stack: if the program has multiple interfaces but does not require an Activity on each interface, you can design each interface as a View. During interface switching, you only need to call the setContentView method of the Activity to set the Contentview of the Activity. To exit the program, you only need to exit the Activity, but you need to design a stack to manage the content view.
You can customize the stack of an Activity and finish all the activities in the stack when the program exits. This method is described in my previous articles.
The essence of the first two methods is that you need to design a stack to manage the interface or Activity, so that the program is more complicated. The method in 3rd is to first let the program go to the Home interface and then kill process: the code is as follows:

[Java]
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_HOME );
Intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP );
StartActivity (intent );
Android. OS. Process. killProcess (Process. myPid ());

Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_HOME );
Intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP );
StartActivity (intent );
Android. OS. Process. killProcess (Process. myPid ());


4. Another method is to use the Android Broadcast mechanism. Register the message for exiting the program in all the activities and call the finish method when receiving the message. Then, the Activity that exits the program function broadcasts the closed message. The Code is as follows:

 

 

[Java]
Package com. kingtone. activity;

Import android. app. Activity;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. content. IntentFilter;
/**
* The parent class of all activities, used to register broadcast events that exit the program,
* Handle received exit program events
* @ Author Administrator
*
*/
Public class CommonActivity extends Activity {

// Broadcast internal class. When a close event is received, call the finish method to end the activity.
Private BroadcastReceiver broadcastReceiver = new BroadcastReceiver (){
@ Override
Public void onReceive (Context context, Intent intent ){
Finish ();
}
};

@ Override
Public void onResume (){
Super. onResume ();
// Register the broadcast in the current activity
IntentFilter filter = new IntentFilter ();
Filter. addAction (GlobalVarable. EXIT_ACTION );
This. registerReceiver (this. broadcastReceiver, filter );
}
}

Package com. kingtone. activity;

Import android. app. Activity;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. content. IntentFilter;
/**
* The parent class of all activities, used to register broadcast events that exit the program,
* Handle received exit program events
* @ Author Administrator
*
*/
Public class CommonActivity extends Activity {

// Broadcast internal class. When a close event is received, call the finish method to end the activity.
Private BroadcastReceiver broadcastReceiver = new BroadcastReceiver (){
@ Override
Public void onReceive (Context context, Intent intent ){
Finish ();
}
};

@ Override
Public void onResume (){
Super. onResume ();
// Register the broadcast in the current activity
IntentFilter filter = new IntentFilter ();
Filter. addAction (GlobalVarable. EXIT_ACTION );
This. registerReceiver (this. broadcastReceiver, filter );
}
}

 

In the Activity (subclass of CommonActivity) to exit the program, the exit code is as follows:

 

 

[Java]
Intent intent = new Intent ();
Intent. setAction (GlobalVarable. EXIT_ACTION); // exit
This. sendBroadcast (intent); // send Broadcast
Super. finish ();
// Exit the background thread and destroy static variables
System. exit (0 );

Intent intent = new Intent ();
Intent. setAction (GlobalVarable. EXIT_ACTION); // exit
This. sendBroadcast (intent); // send Broadcast
Super. finish ();
// Exit the background thread and destroy static variables
System. exit (0 );

 

How to listen for and process the user's press back

 

 

[Java]
Package com. example. android_test01;
 
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. AlertDialog. Builder;
Import android. content. DialogInterface;
Import android. content. DialogInterface. OnClickListener;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. Menu;
 
Public class MainActivity extends Activity {
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}
 
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
System. out. println ("You pressed back1 ");
Show_simple ();
Return super. onKeyDown (keyCode, event );
}
// Call the onKeyDown (int keyCode, KeyEvent event) method first, and then call the onBackPressed () method;
@ Override
Public void onBackPressed (){
// TODO Auto-generated method stub
System. out. println ("call when you press back to exit activity immediately ");
Super. onBackPressed ();
}

 

Public void show_simple ()
{
AlertDialog. Builder builder = new Builder (this );
Builder. setTitle ("welcome to use this software ");
Builder. setPositiveButton ("OK", new OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
// Exit the application;
Finish ();
}
});
Builder. setNegativeButton ("cancel", new OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub

}
});
Builder. create (). show ();


}
}

Package com. example. android_test01;

Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. AlertDialog. Builder;
Import android. content. DialogInterface;
Import android. content. DialogInterface. OnClickListener;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. Menu;

Public class MainActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}

@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
System. out. println ("You pressed back1 ");
Show_simple ();
Return super. onKeyDown (keyCode, event );
}
// Call the onKeyDown (int keyCode, KeyEvent event) method first, and then call the onBackPressed () method;
@ Override
Public void onBackPressed (){
// TODO Auto-generated method stub
System. out. println ("call when you press back to exit activity immediately ");
Super. onBackPressed ();
}
 

 
Public void show_simple ()
{
AlertDialog. Builder builder = new Builder (this );
Builder. setTitle ("welcome to use this software ");
Builder. setPositiveButton ("OK", new OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
// Exit the application;
Finish ();
}
});
Builder. setNegativeButton ("cancel", new OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub

}
});
Builder. create (). show ();


}
}


 

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.