【问题标题】:jquery mobile on phonegap backbutton eventjquery mobile on phonegap 后退按钮事件
【发布时间】:2013-07-04 21:37:06
【问题描述】:

我有以下代码:

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

function onDeviceReady() {
    document.addEventListener("backbutton", onBackKeyDown, false);
}

function onBackKeyDown() {
    if ($.mobile.activePage.attr("id") === "home") {
        e.preventDefault();
        navigator.app.exitApp(); //not working
    }
}

我的onBackKeyDown()函数正在输入,现在我收到了一系列奇怪的事件:

  1. 即使我的$.mobile.activePage.attr("id") === "home" 为真(在 weinre 服务器上测试),我的 if 条件也从未输入过
  2. navigator.app.exitApp 从不工作,这似乎发生在我的整个应用程序中,而不仅仅是这一个功能。
  3. 后退按钮在我的整个应用程序中都没有响应。

知道为什么我会出现这种奇怪的行为吗? Phonegap 2.6、jquery mobile 1.3.0 并在 Android 2.3.7 上进行测试。

【问题讨论】:

  • 你如何调用该函数以及何时调用?

标签: jquery-mobile cordova


【解决方案1】:

在我这里测试

if ($.mobile.activePage.attr("id") === "home") {

完美运行。

“navigator.app.exitApp()”已经不能正常工作了,因为“navigator”指的是浏览器本身,PhoneGap 应用程序不使用浏览器,而是使用 webview。

但是应用程序在每个操作系统版本上的行为方式不同,请尝试以下操作:

if(navigator.app){
        navigator.app.exitApp();
}else if(navigator.device){
        navigator.device.exitApp();
}

【讨论】:

    【解决方案2】:

    我正在使用 cordova 和 jquery mobile 测试 html5 应用程序 - 对于 Android 和 Blackberry,以下代码适用于我:

    var ua = navigator.userAgent;
    window.device = {
    iphone: ua.match(/(iPhone|iPod|iPad)/),
    blackberry7: ua.match(/91|93|97|96|97|9800|9810|9850|9860|99/),
    blackberry10: ua.match(/BB|PlayBook|Tablet/),
    android: ua.match(/Android/)
    }
    //window.device ready for blackberry
    if (window.device.blackberry7){
        document.addEventListener('deviceready', onDeviceReady, false);
    }
    //window.device ready for android
    else {
        window.addEventListener('load', onDeviceReady, false);
    }
    

    然后:

    function onDeviceReady(){
    document.addEventListener("backbutton", onBackKeyDown, false);
    document.addEventListener("menubutton", onMenuKeyDown, false);
    document.addEventListener("searchbutton", onSearchKeyDown, false);
    }
    function onBackKeyDown(){
    if (window.device.blackberry7){blackberry.app.exit();}
    if (!window.device.blackberry7){navigator.app.exitApp();}
    }
    function onMenuKeyDown(){
    if (window.device.blackberry7){alert("BB Menu key");}
    if (!window.device.blackberry7){alert("Menu key");}
    }
    function onSearchKeyDown(){
    if (window.device.blackberry7){alert("BB Search key");}
    if (!window.device.blackberry7){alert("Search key");}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多