Phonegap检测backButton事件失败

时间:2014-02-15 05:48:43

标签: android cordova

当我使用https://build.phonegap.com/在android中构建我的应用程序时,我处理检测后退按钮的代码不起作用。

虽然我使用了以下代码来执行此操作:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet"  href="css/jquery.mobile-1.4.1.min.css">
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/jquery210.js"></script>
<script type="text/javascript" src="js/engine2.js"></script>
<script type="text/javascript" src="js/jqm141.js"></script>
<script type="text/javascript">
function onLoad(){
    document.addEventListener('deviceready', function() {
        navigator.splashscreen.show();
        document.addEventListener("backbutton", ShowExitDialog, false);
    }, false);
}

function ShowExitDialog() {
    if (navigator.notification) {
        navigator.notification.confirm(
            ("Are you sure ?"),
            alertexit,
            'Exit',
            'Yes,No'
        );
    }
}

function alertexit(button){
    if(button=="1" || button==1){
        navigator.app.exitApp();
    }
}
</script>
</head>

<body onLoad="onLoad()">

...

</body>
</html>

按下后退按钮时,我的应用程序没有响应任何内容。如何将检测功能添加到后退按钮??

1 个答案:

答案 0 :(得分:3)

使用这个简单的代码......

 document.addEventListener("deviceready", appReady, false);
        function appReady()
        {   
            document.addEventListener('backbutton', function(e){
                      if (confirm("Press a button!"))
                      {
                     alert("You pressed OK!");
                     navigator.app.exitApp();
                      }
                    else
                      {
                        alert("You pressed Cancel!");
                      }
              }, false);
        }