2013-07-04 20 views

回答

0

像這樣: 編輯

public boolean onKeyUp(int keyCode, KeyEvent event) { 
if (keyCode == KeyEvent.KEYCODE_MENU) { 
    // Create your alertDialog 
     new AlertDialog.Builder(this) 
    .setTitle("Exit ?") 
    .setMessage("Are you sure you want to exit?") 
    .setNegativeButton(android.R.string.no, null) 
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface arg0, int arg1) { 
      //Destroy your current activities 
    Intent i = getBaseContext().getPackageManager() 
        .getLaunchIntentForPackage( getBaseContext().getPackageName()); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(i); 
     } 
    }).create().show(); 
    return true; 
} 
return super.onKeyUp(keyCode, event); 
} 

希望這有助於

+0

喜@adrien塞爾我只是執行你的代碼,但應用程序我以前不不,當我退出按下yes按鈕。 –

+0

這就是爲什麼我寫了//一些東西^^看到我的編輯 – Angudroid

0
  1. 菜單按鈕被棄用! (自android 4以來)
  2. 不要以編程方式退出應用程序。讓android系統管理應用程序生命週期!無需退出應用程序。事實上,你會破壞多任務應用上下文切換(這是Android與iOS相比的一大特性)。因此,用戶無法回到當前的應用程序狀態(屏幕/數據等),因爲應用程序將開始全新的,用戶必須導航到他之前等的地方。
0

Android的設計沒有贊成退出選擇應用程序,而是由操作系統管理它。您可以通過其相應的意圖打開主頁應用:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_MENU)) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Do you want to Exit?").setCancelable(true) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Intent intent = new Intent(Intent.ACTION_MAIN); 
        intent.addCategory(Intent.CATEGORY_HOME); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent); 
       } 
      }); 
       .setNegativeButton("OK", new DialogInterface.OnClickListener() { 
        dialog.dismiss(); 
       }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
    } 
    return true; 
} 
+0

@Fadzhli Azmandin你找到解決方案嗎? –

1

似乎沒有人關心你標記你的問題cordovahtml5!在您onDeviceReady事件中,你的監聽器設置爲菜單按鍵,調navigator.app.exitApp()

function onDeviceReady(){ 
    document.addEventListener("menubutton", closeApp, false); 
} 

function closeApp(){ 
    navigator.app.exitApp(); 
} 

要知道,儘管其他的答案實際上不回答你的問題,他們有重要的信息:

  • 並非所有設備有一個菜單按鈕。
  • 一定要通知用戶你正在關閉應用程序,Adrien顯示對話框的方法非常標準和足夠好。
  • 如果您嘗試發佈了iOS版關閉按鈕的應用程序您的應用程序可能會被拒絕
+0

嗨@gameower,謝謝你的回答。如何在用戶按菜單按鈕時創建菜單退出。不僅僅是在按下菜單按鈕時退出應用程序。 –

+0

那麼,你有聽衆,如果你想在HTML學習[如何添加元素到頁面](http://www.dustindiaz.com/add-and-remove-html-elements-dynamically -with-javascript /),如果你想在android本機代碼上執行它[刪除監聽器並創建菜單](http://www.casperweb.in/phonegap/create-android-native-menu-in-phonegap /)。如果你想讓別人爲你做,我相信你可以聘請一名開發人員。我甚至可以給你發電子郵件給我。 – caiocpricci2

相關問題