PhoneGap - android退出按钮 [英] PhoneGap - android exit on backbutton

查看:148
本文介绍了PhoneGap - android退出按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jquery mobile和cordova来编写RSS阅读器。我的RSS阅读器包含3页(在同一个HTML文档:page1,page2,page3)。我试图重写(硬件)的backbutton行为,所以它会退出程序。为了检查我没有在项目设置中犯任何错误我已经使用PhoneGap示例项目并在Eclipse中加载它。每个示例函数都工作,所以我已经将我的index.html和res文件夹移动到phonegap示例。在我的index.html中,我导入了下面的脚本:

I am trying to program RSS reader using jquery mobile and cordova. My RSS reader consists of 3 pages (in same HTML document: page1, page2, page3). I am trying to override (hardware)backbutton behaviour so it would exit the program. To check that I am not doing any mistakes in project setup I have used PhoneGap example project and loaded it in Eclipse. Every sample function worked so I have moved my index.html and res folder to phonegap example. In my index.html I import the folowing scripts:

<script src="res/jquery-1.7.1.min.js"></script>
<script src="res/jquery.mobile-1.1.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>

,我的main.js文件如下所示:

and my main.js file look like this:

document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('#homepage')){
    e.preventDefault();
    navigator.app.exitApp();
}
else {
    navigator.app.backHistory()
}
}, false);

您可以在第一个代码示例中检查我的脚本的版本。任何想法如何我可以让代码工作,所以它会退出应用程序,当我按下我的Xperia Arc上的按钮?我可以上传我的完整代码,如果需要。

You can check version of my scripts in first code sample. Any ideas on how I could get the code working so it would simply exit app when I press backbutton on my Xperia Arc? I can upload my full code if needed.

编辑:我已经测试了phonegap(cordova)哔哔的功能在我的Android手机,它的工作原理,实施。它必须是在main.js文件中的东西。可能与jquerymobile backbutton函数和phonegap backbutton函数有一些兼容性问题。

I have tested phonegap(cordova) beep function on my android phone and it works so this doesnt have anything with bad script implementation. It must be something in main.js file. Maybe some compatibility issue with jquerymobile backbutton functions and phonegap backbutton function.

推荐答案

您需要等待设备准备添加事件侦听器:

You need to wait for the device to be ready to add the event listener:

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

function onDeviceReady(){
    document.addEventListener("backbutton", function(e){
       if($.mobile.activePage.is('#homepage')){
           e.preventDefault();
           navigator.app.exitApp();
       }
       else {
           navigator.app.backHistory()
       }
    }, false);
}

这篇关于PhoneGap - android退出按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆