2011-03-09 46 views
1

嗨,我有一個問題,在我的應用程序 請參閱下面的代碼以供參考。 我面對的問題是,當我從我的模擬器上點擊返回鍵按鈕時,我想讓我的應用程序問我,你確定要退出嗎?當我按是的話,它應該退出,否則它不應該。 我已經完成了所有的事情,但是當我點擊返回鍵按鈕時,它會顯示一個對話框並自動退出。什麼原因onKeyDown Android中的方法覆蓋

代碼:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 

    AlertDialog.Builder alert_wifi = new AlertDialog.Builder(this); 
      alert_wifi.setMessage("This is a Wi-Fi Service. Your Device does not have an active Wi-Fi Connection. Do you Want to Activate Wi-Fi Connection") 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
       // Action for 'Yes' Button 

        dialog.dismiss(); 

       ViewCamera.isStop = true; 

       } 
       }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
       // Action for 'NO' Button 
       dialog.cancel(); 
       loading_act.finish(); 
       } 
      }); 
      AlertDialog alert = alert_wifi.create(); 
      // Title for AlertDialog 
      alert.setTitle("Caution!"); 
      alert.setIcon(R.drawable.wifi); 
      // Icon for AlertDialog 
      alert.show(); 

    } 

    return super.onKeyDown(keyCode, event); 
} 

可能是什麼問題,請幫助

回答

2

嘗試添加該方法

@Override 
    public void onBackPressed() { 
    ... 
    } 
+0

onBackPressed不會被我解僱。 – 2011-12-23 17:42:36

7
return super.onKeyDown(keyCode, event); 

更改它return true;

調用超級使得活動執行默認行爲,因此它退出。