如何获取Android应用使用PhoneGap的退出preSS时后退按钮上的index.html [英] How to get android app to quit when press back button on index.html using phonegap

查看:170
本文介绍了如何获取Android应用使用PhoneGap的退出preSS时后退按钮上的index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个建立罚款并安装在Android上的应用程序。

然而,当我使用的应用程序和浏览了几页,并返回到index.html的,如果我preSS返回按钮的应用程序不会退出,它只是追溯到一个页面。

如何让一个PhoneGap的应用程序终止/时index.html的退出,然后preSS回来?

什么是退出应用程序通常的做法?

请帮我。

 <!DOCTYPE HTML>
    < HTML和GT;
    < HEAD>
    <间的charset =UTF-8>
    < META HTTP-EQUIV =X-UA-Compatible的内容=IE =边缘>
    < META NAME =视口
        内容=WIDTH =设备宽度,初始规模= 1,最大规模= 1,用户可扩展= 0/>
    < META NAME =视口CONTENT =WIDTH =设备宽度/>
    <标题>< /标题>    <脚本SRC =资源/ jQuery的-1.10.2.min.jsTYPE =应用程序/ JavaScript的>< / SCRIPT>
    <链接rel =stylesheet属性类型=文/ CSS的href =资源/ bootstrap.min.css/>
    <链接rel =stylesheet属性类型=文/ CSS的href =资源/ style.css文件/>
    <脚本SRC =资源/ jquery.mobile-1.4.5.min.jsTYPE =应用程序/ JavaScript的>< / SCRIPT>
    <链接rel =stylesheet属性类型=文/ CSS的href =资源/ jquery.mobile-1.4.5.min.css媒体=屏幕/>
    < /头>
    <脚本>
        变种g_context = 0;
        功能submitFormInPopUp(){
            window.open('','Prvwindow','位置=没有,地位=没有,工具栏=没有,滚动条=是的,宽度= 730,高度= 500');
            document.test.action =preview.jsp
            document.test.target =Prvwindow
            document.test.submit();
        }
        $(函数(){
            document.addEventListener(deviceready,onDeviceReady,FALSE);            $('#mainDiv')隐藏()。            的setInterval(函数(){
                $('#AnimationDiv')隐藏()。                $('#mainDiv)显示()。
            },4000);            VAR参数1 = getQueryVariable(信息);            如果(参数1 =与&!&安培;!参数1 =未定义)
                警报(参数1);
        });
功能onDeviceReady(){
        //注册事件监听器
        document.addEventListener(后退按钮,onBackKeyDown,FALSE);
    }    //处理后退按钮
    //
    功能onBackKeyDown(){
     警报(pressed后退按钮);    }
        功能getQueryVariable(变量){
            变种查询= window.location.search.su​​bstring(1);
            变种瓦尔= query.split(与&);
            对于(VAR I = 0; I< vars.length;我++){
                变种对=瓦尔[I] .split(=);
                如果(对[0] ==变量){
                    返回对[1];
                }
            }
        }
         功能测试(){
            VAR用户名= $(#用户名)VAL()。
            。VAR密码= $(#密码)VAL();
            //警报(用户名);
            //警报(密码);
            如果(!(用户名==''||密码=='')){
            $。员额(g_ipaddress +/ HTRIMS-1 /登录,{
            用户名:用​​户名,密码:密码
            },
            功能(数据){
                //警报(数据);
                //alert(data.message +::+ data.reason);
                如果(data.message ==成功){
                    localStorage.setItem(角色ID,data.id);
                    window.location.assign(services.html)
                }
                其他{
                        的document.getElementById(loginFailed)的innerHTML = data.reason。
                }
            });
            }
        }        < / SCRIPT>    <车身风格=背景色:白色; >
    < D​​IV ID =mainDiv的风格=背景色:白色;>
            < D​​IV数据角色=头的风格=背景颜色:#004255;高度:50像素; BORDER-TOP-宽度:0像素;>            <一个数据iconpos =左ID =后退按钮的风格=背景颜色:#004255;颜色:白色;文字阴影:0像素0像素#FF0000;边框:0像素;填充:0像素,填充顶:为5px;的onclick =>< IMG SRC =图标/ bel_icon.ico的风格=高度:32PX;高度:32PX;/>< / A>
            < H1风格=颜色:白色,字体大小:20像素;文字阴影:0像素0像素#FF0000;> HTRIMS< / H1>            < / DIV>        <形式为GT;
            < D​​IV CLASS =容器ID =登录>
                < D​​IV CLASS =行>
                    < D​​IV CLASS =内容>
                        < D​​IV CLASS =节的风格=的margin-top:70像素;背景颜色:白色;>
                                <中心及GT;                                    <输入名称=用户名占位符=用户名级=表格控ID =用户名的风格=宽度:100%; FONT-SIZE:知识+;文本对齐:中心;背景色: #E9DBD6;文字阴影:0像素0像素#FF0000;的onClick =检查(); />                                < BR>< BR>                                    <输入类型=密码
                                        NAME =密码占位符=密码级=表格控
                                        ID =密码的风格=宽度:100%; FONT-SIZE:知识+;文本对齐:中心;背景颜色:#E9DBD6;文字阴影:0像素0像素#FF0000; />                                < /中心及GT;
                                < BR>< BR>< BR>
                                <中心><跨度值=LOGINNAME =登录的onclick =测试()级=BTN BTN-小学的风格=文本对齐:中心;宽度:100%;高度:40像素;文字阴影:0像素0像素#FF0000; FONT-SIZE:15px的框阴影:5像素5像素为5px#888888;背景颜色:#00ADEE;颜色:白色; />登录和LT; /中心及GT;                                < BR>                                    < BR><中心及GT;<跨度
                                    风格=颜色:红色; ID =loginFailed>< / SPAN>< /中心及GT;
                                    < BR><中心及GT;<跨度
                                    风格=颜色:红色; ID =loginFailed1>< / SPAN>< /中心及GT;
                                    < D​​IV ID =错误级=错误>< / DIV>
                                    < / DIV>< / DIV>
                        < / DIV>
                    < / DIV>
        < /表及GT;
    < / DIV>    < D​​IV ID =AnimationDiv>        <中心及GT;
     < IMG SRC =Bharat_Electronics_logo.png的风格=WIDTH:304px;高度:200像素;的margin-top:为200px;>
    < /中心及GT;    < / DIV>    <! - <脚本类型=文/ JavaScript的SRC =loginscript.js>< / SCRIPT> - >
    < /身体GT;
    < / HTML>


解决方案

试试这个..

  document.addEventListener(后退按钮功能(E){
如果($。mobile.activePage.is('#主页)){
    / *
     事件preventDefault / stopPropagation不需要如添加后退按钮
      监听器本身覆盖默认行为。请参考下PhoneGap的链接。
    * /
    //e.$p$pventDefault();
    navigator.app.exitApp();
}
其他{
    navigator.app.backHistory()
}
},FALSE);

这是链接

http://docs.phonegap.com/en/ 2.0.0 / cordova_events_events.md.html#后退按钮

I have an app that builds fine and installs on Android.

However when I use the app and navigate to a few pages and back to index.html if I press back button the app does not quit, it just goes back to a page.

How do you get a PhoneGap app to terminate / quit when on index.html and then press back?

WHat is the normal practice for quitting the app?

Please help me.

 <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
    <meta name="viewport" content="width=device-width" />
    <title></title>

    <script src="resources/jquery-1.10.2.min.js" type="application/javascript"></script>
    <link rel="stylesheet" type="text/css" href="resources/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="resources/style.css"/>
    <script src="resources/jquery.mobile-1.4.5.min.js" type="application/javascript"></script>
    <link rel="stylesheet" type="text/css" href="resources/jquery.mobile-1.4.5.min.css" media="screen" />
    </head>
    <script>
        var g_context=0;


        function submitFormInPopUp() {
            window.open('', 'Prvwindow','location=no,status=no,toolbar=no,scrollbars=yes,width=730,height=500');
            document.test.action = "preview.jsp"
            document.test.target = "Prvwindow"
            document.test.submit();
        }


        $(function() {
            document.addEventListener("deviceready", onDeviceReady, false);

            $('#mainDiv').hide();

            setInterval(function(){
                $('#AnimationDiv').hide();

                $('#mainDiv').show();


            }, 4000);

            var param1 = getQueryVariable("message");

            if (param1 != "" && param1 != undefined)
                alert(param1);


        });
function onDeviceReady() {
        // Register the event listener
        document.addEventListener("backbutton", onBackKeyDown, false);
    }

    // Handle the back button
    //
    function onBackKeyDown() {
     alert("Pressed back button");

    }
        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for ( var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
        }


         function test(){
            var username = $("#username").val();
            var password = $("#password").val();
            //alert(username);
            //alert(password);
            if(!(username == '' || password == '')){
            $.post(g_ipaddress + "/HTRIMS-1/login",{
            username : username ,password : password
            },  
            function(data){
                //alert(data);
                //alert(data.message + "::" + data.reason);
                if(data.message=="Success"){
                    localStorage.setItem("roleid",data.id);
                    window.location.assign("services.html")
                }
                else{
                        document.getElementById("loginFailed").innerHTML=data.reason;
                }
            });
            }


        } 

        </script>

    <body style="background-color:white;" >
    <div id="mainDiv" style="background-color:white;">
            <div data-role="header" style="background-color:#004255;height:50px;border-top-width: 0px;">

            <a  data-iconpos="left"  id = "backbutton" style="background-color:#004255;color:white;text-shadow: 0px 0px #ff0000;border:0px;padding:0px;padding-top:5px;" onclick = "" ><img  src= "icons/bel_icon.ico" style = "height:32px;height:32px;"/></a>
            <h1 style = "color:white;font-size:20px;text-shadow: 0px 0px #ff0000;">HTRIMS</h1>

            </div>



        <form>
            <div class="container" id="login">
                <div class="row">
                    <div class="content">
                        <div class="section" style="margin-top: 70px;background-color:white;">
                                <center>

                                    <input name="username" placeholder="USER NAME" class="form-control" id="username" style = "width:100%;font-size:17px;text-align:center;background-color:#E9DBD6;text-shadow: 0px 0px #ff0000;" onClick="check();" />

                                <br><br>

                                    <input type="password"
                                        name="password" placeholder="PASSWORD" class="form-control"
                                        id="password" style = "width:100%;font-size:17px;text-align:center;background-color:#E9DBD6;text-shadow: 0px 0px #ff0000;" />

                                </center>
                                <br><br><br>
                                <center><span  value="LOGIN" name="Login" onclick="test()" class="btn btn-primary" style = "text-align:center;width:100%;height:40px;text-shadow: 0px 0px #ff0000;font-size:15px;box-shadow: 5px 5px 5px #888888;background-color:#00ADEE;color:white;" />LOGIN</center>

                                <br>

                                    <br><center><span
                                    style="color: red;" id = "loginFailed"></span></center>
                                    <br><center><span
                                    style="color: red;" id = "loginFailed1"></span></center>
                                    <div id="error" class="error"></div>
                                    </div></div>
                        </div>
                    </div>
        </form>
    </div>









    <div id="AnimationDiv">

        <center>
     <img src="Bharat_Electronics_logo.png"  style="width:304px;height:200px;margin-top:200px;">
    </center>

    </div>

    <!--<script type="text/javascript" src="loginscript.js"></script>-->
    </body>
    </html>

解决方案

try this..

document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('#homepage')){
    /* 
     Event preventDefault/stopPropagation not required as adding backbutton
      listener itself override the default behaviour. Refer below PhoneGap link.
    */
    //e.preventDefault();
    navigator.app.exitApp();
}
else {
    navigator.app.backHistory()
}
}, false);

this is link

http://docs.phonegap.com/en/2.0.0/cordova_events_events.md.html#backbutton

这篇关于如何获取Android应用使用PhoneGap的退出preSS时后退按钮上的index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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