프로그래밍 세계/Android

Back-key 클릭시 프로그램 종료 코드

깡다구짱 2011. 7. 11. 21:12



 



import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.KeyEvent;


public class MyFinance extends Activity {

....



 // Back-key 클릭시 프로그램 종료 코드
 public boolean onKeyDown(int keyCode, KeyEvent event){
  switch(keyCode){
  case KeyEvent.KEYCODE_BACK:
   String alertTitle = getResources().getString(R.string.app_name);

   new AlertDialog.Builder(MyFinance.this)
   .setTitle(alertTitle)
   .setMessage("종료하겠습니까?")
   .setPositiveButton("예", new DialogInterface.OnClickListener(){
    
    public void onClick(DialogInterface dialog, int which){
     moveTaskToBack(true); // 본Activity finish후 다른 Activity가 뜨는 걸 방지.
     finish();
     // android.os.Process.killProcess(android.os.Process.myPid()); 
     // -> 해당 어플의 프로세스를 강제 Kill시킨다.
    }    
   })
   .setNegativeButton("아니오", null)
   .show();
  }
   
  return true;
 }



....
}