首页 文章

Cordova backbutton Event Never Firing

提问于
浏览
1

根据 deviceready 事件后 false 的参数将事件监听器附加到 backbutton 的文档,我正在覆盖我的设备后退按钮 .

我是这样做的:

// Smart App object
define([
    'routes',
    'cordova',
    'angular',
    'angularRoute',
    'angularResource',
    'angularTouch',
    'config',
    'controllers',
    'services',
    'directives',
    'helpers'
    ], function(appRoute) {
        var oApp = {
            _app: {},

            init: function() {
                console.log('init');
                document.addEventListener('deviceready', this.onDeviceReady, false);
            },

            onDeviceReady: function() {
                console.log('ok device ready');
                document.addEventListener('backbutton', function() {
                    console.error('deviceback start');
                    angular.element('.ng-scope').scope().back();
                    console.error('deviceback end');
                }, false);
                // ...
            }
        // ...

我疯狂地想弄清楚为什么按下设备按钮没有触发我附加的这个 backbutton 事件,我甚至都没有在控制台中看到 console.error 消息 . 我现在在Android上测试我还没有't tested on any of the other Phone OS' es .

console.log('ok device ready') 确实触发并且我的应用程序正常工作,设备_2515426被覆盖,因为它的默认功能没有发生,但是如上所述,我的功能也没有发生 .

我读了其他stackoverflow topcis并且他们说Cordova版本获得了修复,但是我的Cordova版本在他们之后很多,我的 .cordova/config.json 文件告诉我:

{
    "lib": {
        "www": {
            "id": "com.tigoenergy.smart",
            "version": "3.5.0"
        }
    }
}

它是 3.5 然后我的 info.txt 告诉我它是 5.0.0

Node version: v0.10.25

Cordova version: 5.0.0

Config.xml file: 

<?xml version="1.0" encoding="UTF-8"?>

<!-- config.xml reference: https://build.phonegap.com/docs/config-xml -->
<widget xmlns     = "http://www.w3.org/ns/widgets"
        xmlns:gap = "http://phonegap.com/ns/1.0"

有没有人有任何想法?

3 回答

  • 1

    尝试使用正常使用cordova事件的方式 .

    // device APIs are available
    //
    function onDeviceReady() {
        // Register the event listener
        document.addEventListener("backbutton", onBackKeyDown, false);
    }
    
    // Handle the back button
    //
    function onBackKeyDown() {
        //Add your back button implementation.
    }
    

    官方文件here

    修改代码

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/html">
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <title>Tigo SMART Installation</title>
    <script type="text/javascript" src="apps/installation/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="apps/installation/js/index.js"></script>
    <script type="text/javascript" src="apps/installation/js/fastclick.min.js"></script>
    <script type="text/javascript" src="apps/installation/js/sha512.js"></script>
    <script type="text/javascript" src="bower_components/uri.js/src/URI.min.js"></script>
    <script type="text/javascript" src="js/extlogin.js"></script>
    <link rel="stylesheet" href="css/app.css">
    <link rel="stylesheet" type="text/css" href="apps/installation/css/index.css" />
    
    <script type="text/javascript" charset="utf-8">
    
    $(document).ready(function(){
        //mycode
        console.log('ok loaded');
        document.addEventListener("deviceready", onDeviceReady, false);
    });
    // Wait for device API libraries to load
    //
    /*function onLoad() {
    console.warn('ok loaded')
    document.addEventListener("deviceready", onDeviceReady, false);
    }
    */
    
    // device APIs are available
    //
    function onDeviceReady() {
    // Register the event listener
    console.log('ok device ready');
    document.addEventListener("backbutton", onBackKeyDown, false);
    }
    
    // Handle the back button
    //
    function onBackKeyDown() {
    console.log('ok backey downed');
    }
    
    </script>
    </head>
    <body>
    
    <div ng-view></div>
    <script data-main="bin/SmartApp" src="bower_components/requirejs/require.min.js"></script>
    
    <img style="opacity:0;visibility:hidden;" class="loader" src="img/loader.gif" />
    </body>
    </html>
    

    很少有参考链接herehere

  • 0

    问题是我在iframe中导入cordova.js,即使它已经在父窗口范围内 . 我不知道它会破坏它,并认为我必须在iframe中导入cordova.js . 从iframe中删除修复它 .

  • 1

    试试这个

    $(document).ready(function () {
    
            document.addEventListener("backbutton", onBackKeyDown, false);
        });
        function onBackKeyDown() {
            alert('ok backey downed');
        }
    

    确保这些文件在您的页面中是链接

    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="scripts/platformOverrides.js"></script>
    <script type="text/javascript" src="scripts/index.js"></script>
    

相关问题