Salir de la aplicación cuando haga clic en el botón en Android phonegap?


Soy nuevo en phonegap. He preparado una solicitud de muestra. Mi aplicación tiene 2 páginas, la primera página tiene un botón, cuando se hace clic en la segunda página se abrirá. Está funcionando bien usando lo siguiente

         function callAnothePage()
         {
            window.location = "test.html";
         }

La segunda página tiene un botón cuando se hace clic quiero salir de la aplicación. He utilizado la siguiente

       function exitFromApp()
       {
            navigator.app.exitApp();
       }

test.html código,

<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
    <script type="text/javascript" charset="utf-8">  
        function onLoad()
        {
            console.log("device reday !!!!")
            document.addEventListener("deviceready", onDeviceReady, true);
        }
        function exitFromApp()
        {
            console.log("in button");
            navigator.app.exitApp();
        }
    </script>
</head>

<body onload="onLoad();">
    <h4><center>Login Page</center></h4>

    <input type="submit" onclick="exitFromApp()" value="exit"/>

</body>
</html>

Pero no está funcionando.

Author: Lucky Kleinschmidt, 2012-09-06

7 answers

Pruebe este código.

<!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 " con su phonegap js .

 61
Author: Chirag,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-06 10:26:47

Hay diferentes formas de cerrar la aplicación, dependiendo de:

if (navigator.app) {
    navigator.app.exitApp();
} else if (navigator.device) {
    navigator.device.exitApp();
} else {
    window.close();
}
 12
Author: Ujjwal Kumar Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-24 13:02:59
navigator.app.exitApp();

Agregue esta línea donde desea salir de la aplicación.

 11
Author: Anil Singhania,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-14 23:53:26

@ Pradip Kharbuja

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

exitApp:function() {
  console.log("Device.exitApp() is deprecated. Use App.exitApp().");
  app.exitApp();
}
 3
Author: Shadow,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-24 08:12:13
if(navigator.app){
        navigator.app.exitApp();
}else if(navigator.device){
        navigator.device.exitApp();
}
 2
Author: Stevko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-16 22:09:40

Lo siento, no puedo responder en el comentario. solo para tu información, estos códigos

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

Confirmo que no funciona. uso phonegap 6.0.5 y cordova 6.2.0

 1
Author: Jamz D. Mozac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-04 09:01:39

Usé una combinación de lo anterior porque mi aplicación funciona tanto en el navegador como en el dispositivo. El problema con el navegador es que no le permitirá cerrar la ventana de un script a menos que su aplicación fue abierta por un script (como 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
Author: Helzgate,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-05 16:53:44