Phonegap 后退按钮 i android 未被捕获

Phonegap back button i android not catched

我正在开发 Phonegap 应用程序。该应用程序已准备就绪,现在我想在按下硬件后退时添加一个后退按钮事件(在 android 中)。

我浏览了很多链接都没有成功。

任何人都可以告诉我在 android 的 Cordova 应用程序中捕获后退按钮事件的确切方法是什么。我的代码如下。

index.html

 <script type="text/javascript">
  document.addEventListener("deviceready", onDeviceReady, false);

  alert("====device ready----");
  // PhoneGap is loaded and it is now safe to make calls PhoneGap methods
  function onDeviceReady() {
      // Register the event listener
      document.addEventListener("backbutton", onBackKeyDown, false);
      alert("===back button initializing==");
  }

  // Handle the back button
  function onBackKeyDown() {
    alert("====BACK PRESSED====");
  }  
</script>

当我按下后退按钮时,没有任何反应。

谁能告诉我我犯了什么错误。请它浪费了 6 个小时。 :(

make sure you have installed codova dialogs plugin:-

For example:-

cordova plugin install org.apache.cordova.dialogs

and also add following line:-

document.addEventListener("backbutton", backKeyDown, true);
                navigator.app.overrideBackbutton(true);
                function backKeyDown() {//code here
                }       

请告诉我现在使用的cordova版本是多少。如果您使用的是 cordova 版本 >=4,请在 cordova.js 中进行以下更改,它将起作用

//var APP_PLUGIN_NAME = Number(cordova.platformVersion.split('.')[0]) >= 4 ? 'CoreAndroid' : 'App';
var APP_PLUGIN_NAME = 'App';