Ionic/Cordova menubutton event not called

☆樱花仙子☆ 提交于 2019-12-10 13:07:17

问题


I am trying to monitor the Menu Button on Android (4.4.2 - Samsung S3), but the Ionic event (nor the underlying Cordova event) is not firing:

$ionicPlatform.on("menubutton", function () {
  // do our stuff here (never gets called)
});

Has anyone been able to make this work? Running Ionic platform 1.0.0, and all other events are firing as expected.


回答1:


The docs are missing a line.

document.addEventListener("deviceready", function() {
    ...
    navigator.app.overrideButton("menubutton", true);  // <-- Add this line
    document.addEventListener("menubutton", yourCallbackFunction, false);
    ...
}, false);

https://issues.apache.org/jira/browse/CB-9949#comment-14989073




回答2:


Try this: in the .run()

$ionicPlatform.ready(function() {
//...
     if (window.cordova) {
        $cordovaSplashscreen.hide();
        document.addEventListener("menubutton", myApp.onHardwareMenuKeyDown, false);
     }
/...

Then in the controller:

 $scope.onHardwareMenuKeyDown = function() {
    alert('menu button is working');
 }

Another way to do something:

angular.module('myApp', ['ngCordova', 'ionic', 'myApp.controllers'])
  .run(function($ionicPlatform, $rootScope, $state, $localstorage,$ionicSideMenuDelegate ) {

     $ionicPlatform.ready(function() {

      document.addEventListener("menubutton", onMenuKeyDown, false);

       function onMenuKeyDown() {
          console.log("some menu pops pup!! ");
          // here change the view , etc... 
          $rootScope.$apply();
        }

  });

})


来源:https://stackoverflow.com/questions/30790419/ionic-cordova-menubutton-event-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!