1

我有以下代码:

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

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

4

2 回答 2

0

我正在使用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");}
}
于 2013-08-29T00:12:21.533 回答
0

在我的测试中

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();
}
于 2013-07-05T00:11:15.410 回答