Phonegap和Jquery mobile - 退出带警报的应用程序

时间:2015-01-23 08:11:15

标签: jquery cordova

我想退出我的应用程序并发出“是”或“否”警报。当您在应用程序的索引端时,我目前有一个退出应用程序的脚本。

<script language="javascript">

document.addEventListener("backbutton", function(e){
    if($.mobile.activePage.is('#index')){
        /* 
         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);

</script>

1 个答案:

答案 0 :(得分:2)

使用带有确认

的对话框插件

使用cordova CLI安装它,如果你还没有它:

cordova plugin add org.apache.cordova.dialogs

如果你使用phonegap build,请将它放在你的config.xml上:

<gap:plugin name="org.apache.cordova.dialogs" version="0.2.10" />

你的代码

<script language="javascript">

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

        navigator.notification.confirm(
            "Do you want to exit the app?",
            function (button) {
              if (button==2) {
                navigator.app.exitApp();
              }
            }
            ,
            "EXIT",
            ["Cancel","OK"]
        );
    }
    else {
        navigator.app.backHistory()
    }
}, false);

</script>