【问题标题】:backbutton confirm exit app android + phonegap + jquery后退按钮确认退出应用程序android + phonegap + jquery
【发布时间】:2013-08-12 10:15:54
【问题描述】:

如何在 Phonegap 在线系统中使用 jQuery Mobile 处理 Android 中的物理后退按钮? 我想显示确认退出应用程序(是 - 否)。我尝试了很多方法,但没有任何效果。

【问题讨论】:

    标签: android jquery mobile cordova


    【解决方案1】:

    试试这个:

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady() {
        document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
    }
    
    function onBackKeyDown(e) {
        e.preventDefault();
        navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No"); 
        // Prompt the user with the choice
    }
    
    function onConfirm(button) {
        if(button==2){//If User selected No, then we just do nothing
            return;
        }else{
            navigator.app.exitApp();// Otherwise we quit the app.
        }
    }
    

    【讨论】:

      【解决方案2】:

      第一次点击时设置exitApp=true,所以第二次点击后退按钮会退出应用。 但是设置一个间隔来将exitApp的状态更改为false。 因此,当在 1 秒内单击两次后退按钮时,将退出应用程序。

      document.addEventListener('deviceready', function() {
          var exitApp = false, intval = setInterval(function (){exitApp = false;}, 1000);
          document.addEventListener("backbutton", function (e){
              e.preventDefault();
              if (exitApp) {
                  clearInterval(intval) 
                  (navigator.app && navigator.app.exitApp()) || (device && device.exitApp())
              }
              else {
                  exitApp = true
                  history.back(1);
              } 
          }, false);
      }, false);
      

      【讨论】:

      • 制作精良。值得注意的是,在这个流程中,应用程序不会从用户第一次点击后退按钮开始等待 1 秒,而是等待下一个间隔滴答声。在第一次点击时创建超时可能更好。
      【解决方案3】:

      //设备就绪函数

      document.addEventListener('deviceready', function() {
      
          document.addEventListener("backbutton", ShowExitDialog, false);
      
      }, false);
      

      //按下后退按钮时的对话框

       function ShowExitDialog() {
              navigator.notification.confirm(
                      ("Do you want to Exit?"), // message
                      alertexit, // callback
                      'My APp', // title
                      'YES,NO' // buttonName
              );
      
          }
      

      //调用退出函数

       function alertexit(button){
      
              if(button=="1" || button==1)
              {
      
                  device.exitApp();
              }
      
      }
      

      【讨论】:

        【解决方案4】:

        不确定 phonegap 是否发生了变化,但 Suhas 和 geet 的解决方案几乎对我有用。 (但绝对给了我让它工作所需的东西,谢谢!)后退按钮基本上坏了。

        以下是使它对我有用的调整:

        加载您的应用后,请执行以下操作:

                document.addEventListener("backbutton", onBackKeyDown, false);//hijack the backbutton
        
                function onBackKeyDown(e){
                    var page = $.mobile.activePage.attr('id');
                    xStat.rec("back button clicked from page:" + page);
                    if (page == 'menuPage'){//are you on the 'root page' from which phonegap will exit?
                        e.preventDefault();
                        $.mobile.changePage('#aboutToExitAppPage');
                    } else {
                        window.history.back();//restore normal back button functionality
                    }
                }
        
        
        //somewhere else in your code for the "aboutToExit app" page
                $('#aboutToExitAppPage').on('pageinit', function(){
                    $(this).find('#exitApp').on('click', function(){
                        navigator.app.exitApp();//quit the app.
                    });
                });
        

        和 HTML

        <div data-role="page" id="aboutToExitAppPage">
            <div data-role="header" id="" data-position="inline" data-backbtn="true" >
                <h1 class=>About to exit app</h1>
            </div>
            <div data-role="content" style="width:100%; height:100%; padding:0;">
                <ul id="" data-role="listview" data-inset="true" data-theme="c" data-dividertheme="b" data-role="fieldContain">
                    <input id="exitApp" class="" type="button" value="Exit" data-theme="">
                    <a href="#menuPage" data-role='button'>Main Menu</a>
                </ul>
            </div>
        </div>
        

        【讨论】:

        • “代码中的其他地方”是什么意思?没用。
        【解决方案5】:

        如果用户点击是,则退出应用程序。 首先通过运行cordova plugin add cordova-plugin-dialogs安装对话框插件

        document.addEventListener("deviceready", onDeviceReady, false);
        
        function onDeviceReady() {
            document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
        }
        
        function onBackKeyDown(e) {
            e.preventDefault();
            navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No"); 
            // Prompt the user with the choice
        }
        
        function onConfirm(button) {
            if(button==1){//If User selected Yes, then exit app
                navigator.app.exitApp();
            } else {
                return;
            }
        }
        

        【讨论】:

          【解决方案6】:

          试试这个:

          // When Device ready
          document.addEventListener('deviceready', function() {
              document.addEventListener("clickBackbutton", ExitDialogPrompt, false);
          }, false); 
          
          function ExitDialogPrompt() {
              navigator.notification.confirm(
                  ("Do you want to Exit?"), // message
                  prompt, // callback
                  'Your title', // title
                  'YES,NO' // button Name
              );
          }
          
          function alertexit(button){
              if (button=="0" || button==1) {
                 navigator.app.exitApp();
              }
          }
          

          【讨论】:

            【解决方案7】:

            这对我有用。

            <script type="text/javascript" src="cordova.js"></script>
            <script type="text/javascript" src="js/index.js"></script>
            

            使用这个

            <script type="text/javascript">
                document.addEventListener("deviceready", onDeviceReady, false);
            
                    function onDeviceReady(){
                        document.addEventListener("backbutton", function(e) {
                            e.preventDefault();
            
                                    if(confirm("Exit App?")) {
                                        navigator.app.exitApp();
                                    }
                        }, false);
                    }
                </script>
            

            <script type="text/javascript">
            document.addEventListener("deviceready", onDeviceReady, false);
            
                function onDeviceReady(){
                    document.addEventListener("backbutton", function(e) {
                        e.preventDefault();
            
                        press = press + 1;
                        if(press == 1) {
                            alert('Press again to exit');
                        }
            
                        if(press == 2) {
                            navigator.app.exitApp();
                        }
                    }, false);
                }
            </script>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-02-08
              • 2012-09-02
              • 2014-01-02
              • 2019-08-20
              • 1970-01-01
              • 2014-05-04
              • 1970-01-01
              相关资源
              最近更新 更多