Exit from app when click button in android phonegap?

前端 未结 7 1746
时光取名叫无心
时光取名叫无心 2020-12-28 11:13

I am new to phonegap. I have prepared one sample application. My application has 2 pages, the first page has one button, when clicked the second page will open. It is workin

相关标签:
7条回答
  • 2020-12-28 11:57
    navigator.app.exitApp();
    

    add this line where you want you exit the application.

    0 讨论(0)
  • 2020-12-28 12:01

    sorry i can't reply in comment. just FYI, these codes

    if (navigator.app) {
    navigator.app.exitApp();
    }
    else if (navigator.device) {
      navigator.device.exitApp();
    }
    else {
              window.close();
    }
    

    i confirm doesn't work. i use phonegap 6.0.5 and cordova 6.2.0

    0 讨论(0)
  • 2020-12-28 12:06

    @Pradip Kharbuja

    In Cordova-2.6.0.js (l. 4032) :

    exitApp:function() {
      console.log("Device.exitApp() is deprecated. Use App.exitApp().");
      app.exitApp();
    }
    
    0 讨论(0)
  • 2020-12-28 12:09

    Try this code.

    <!DOCTYPE HTML>
    <html>
      <head>
        <title>PhoneGap</title>
    
            <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script>      
            <script type="text/javascript" charset="utf-8">
    
                function onLoad()
                {
                      document.addEventListener("deviceready", onDeviceReady, true);
                }
    
                function exitFromApp()
                 {
                    navigator.app.exitApp();
                 }
    
            </script>
    
        </head>
    
        <body onload="onLoad();">
           <button name="buttonClick" onclick="exitFromApp()">Click Me!</button>
        </body>
    </html>
    

    Replace src="cordova-1.5.0.js" with your phonegap js .

    0 讨论(0)
  • 2020-12-28 12:11

    I used a combination of the above because my app works in the browser as well as on device. The problem with browser is it won't let you close the window from a script unless your app was opened by a script (like browsersync).

            if (typeof cordova !== 'undefined') {
                if (navigator.app) {
                    navigator.app.exitApp();
                }
                else if (navigator.device) {
                    navigator.device.exitApp();
                }
            } else {
                window.close();
                $timeout(function () {
                    self.showCloseMessage = true;  //since the browser can't be closed (otherwise this line would never run), ask the user to close the window
                });
            }
    
    0 讨论(0)
  • 2020-12-28 12:16
    if(navigator.app){
            navigator.app.exitApp();
    }else if(navigator.device){
            navigator.device.exitApp();
    }
    
    0 讨论(0)
提交回复
热议问题