0

I'm trying to capture device events in my Android Cordova app. But, except the deviceReady event , no other event is getting fired. I'm using the event as given in the Cordova documentation after the deviceReady event. My Cordova version 6.4.0

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {

  document.addEventListener("backbutton", function(e){
    // My Code here
  });

  document.addEventListener("volumedownbutton", onVolumeDownKeyDown, false);

  function onVolumeDownKeyDown() {
     console.log('volume downn')
    // Handle the volume down button
  }

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

  function onMenuKeyDown() {
   console.log('menu button')
      // Handle the back button
  }

  document.addEventListener("volumeupbutton", onVolumeUpKeyDown, false);

  function onVolumeUpKeyDown() {
    console.log('volume up button')
     // Handle the volume up button
  }
};
Supradeep
  • 3,246
  • 1
  • 14
  • 28
  • Are these event-handlers defined within the deviceready-function or outside it? As it is shown [here](https://cordova.apache.org/docs/de/latest/cordova/events/events.backbutton.html#vollst%C3%A4ndiges-beispiel) for the backbutton-event. – Blauharley Dec 25 '16 at 15:15
  • @Blauharley They are defined after the deviceReady event is triggered as given in that link. – Supradeep Dec 25 '16 at 15:19
  • You inserted code is ok, nevertheless there is still code missing that calls this code too early. How did you wrap these handlers? – Blauharley Dec 25 '16 at 15:51
  • Please check my updated code. That is how I am calling those events. – Supradeep Dec 25 '16 at 16:18
  • I've already your your code within my deviceready-function and it worked with me. This code is defined somewhere too early. – Blauharley Dec 25 '16 at 16:20
  • You mean I should define them before the deviceReady event too ? – Supradeep Dec 25 '16 at 16:22
  • Can anyone help me debug this issue please ? – Supradeep Dec 26 '16 at 06:17
  • @suzo this is the code i m using and it works. function onDeviceReady() { document.addEventListener("backbutton", onBackButton, false); } function onBackButton(e) { e.preventDefault(); navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No"); } – Gandhi Dec 26 '16 at 09:06
  • @Gandhi : Thanks for that, I too used the same code, but it's not working for me. Not only backbutton, but no event is working except deviceReady. – Supradeep Dec 26 '16 at 09:14
  • @suzo can you post your complete code. Cos in some other question also you have raised the same concern but that uses angular. I m able to make it work in my vanilla cordova project. – Gandhi Dec 26 '16 at 09:16
  • @Gandhi: http://stackoverflow.com/questions/41315616/android-cordova-backbutton-event-not-triggering , Code in that question is exactly how I'm using. – Supradeep Dec 26 '16 at 10:18
  • 1
    As @Blauharley said, your function is calls this code too early, just add timeout into different listeners with minimum of 1 second time – Kirankumar Dafda Dec 26 '16 at 10:56
  • @suzo try calling deviceready event listener inside document ready function – Gandhi Dec 26 '16 at 11:32
  • @suzo i suggest you to look at this sample -https://github.com/gandhirajan/Cordova_InAppBrowser This may not be relevant to you but the project did use backbutton event and its a tested sample. This should help – Gandhi Dec 26 '16 at 11:44
  • @KirankumarDafda: I've already tried timeout too, didn't work. – Supradeep Dec 26 '16 at 15:19

1 Answers1

0

See one question here where updating cordova version to 6.4.0 was giving an error into same project which was working fine with version 6.2.0

And the solution was to downgrade the version of cordova helped him to recover the project again.

I have created one test project and putted this index.html which working perfect for me, just try adding the same into your test project and let us if this is working or not

My Cordova version 6.2.0

<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="cordova.js"></script> 
        <script>
            document.addEventListener("deviceready", onDeviceReady, false);
            function onDeviceReady(){
                setTimeout(function(){
                    try{
                        document.addEventListener('backbutton', function(e){    
                            alert("Back Button Clicked")
                        }, false);
                    }catch(e){
                        alert(e);
                    }
                }, 500);
            }
        </script>
    </head>
    <body>
        <div>
            <h1>Click Android Back button to see event fired or not</h1>
        </div>
    </body>
</html>
Community
  • 1
  • 1
Kirankumar Dafda
  • 2,354
  • 5
  • 29
  • 56