Detailed explanation of Android program exit and false exit background running

Source: Internet
Author: User

Public void onBackPressed (){
AlertDialog. Builder builder = new Builder (LawdaquanActivity. this );
Builder. setIcon (R. drawable. imagebutton5 );
Builder. setTitle ("prompt ");
Builder. setMessage ("Do you really want to exit the legal information software? ");
Builder. setPositiveButton ("yes ",
New android. content. DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Intent I = new Intent (Intent. ACTION_MAIN );

I. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

I. addCategory (Intent. CATEGORY_HOME );

StartActivity (I );

}
});
Builder. setNegativeButton ("no ",
New android. content. DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Dialog. dismiss ();
}
});
Builder. create (). show ();

}

 

------------- Quit
Exit application implementation: you can write a method by yourself, for example:

Public void exitProgrames {

Intent startMain = new Intent (Intent. ACTION_MAIN );

StartMain. addCategory (Intent. CATEGORY_HOME );

StartMain. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

StartActivity (startMain );

Android. OS. Process. killProcess (android. OS. Process. myPid ());

 


-------------- Rewrite ONKEYDOWN
Public boolean onKeyDown (int keyCode, KeyEvent event ){

If (keyCode = KeyEvent. KEYCODE_BACK ){
New AlertDialog. Builder (this)
. SetIcon (R. drawable. title)
. SetTitle ("Exit software ")
. SetMessage ("withdraw from legal instruments software ")
. SetPositiveButton ("no ",
New DialogInterface. OnClickListener (){

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

}
}). SetNegativeButton ("exit", new OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
Finish ();
}
}). Create (). show ();
}

Else {
Return super. onKeyDown (keyCode, event );

}

// TODO Auto-generated method stub
Return super. onKeyDown (keyCode, event );
}


----------- False logout
Intent I = new Intent (Intent. ACTION_MAIN );

I. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

I. addCategory (Intent. CATEGORY_HOME );

StartActivity (I );
Finish ();

 

 

From the column of gongzibai

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.