单击android phonegap中的按钮时退出应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12297525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exit from app when click button in android phonegap?
提问by user1651572
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 working fine using the following
我是 phonegap 的新手。我准备了一个示例应用程序。我的应用程序有 2 页,第一页有一个按钮,单击时将打开第二页。使用以下方法可以正常工作
function callAnothePage()
{
window.location = "test.html";
}
The second page has one button, when clicked I want to exit from the application. I used the following
第二页有一个按钮,点击后我想退出应用程序。我使用了以下
function exitFromApp()
{
navigator.app.exitApp();
}
test.html
code,
test.html
代码,
<!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>
But it is not working.
但它不起作用。
回答by Chirag
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 .
用你的 phonegap js替换src="cordova-1.5.0.js"。
回答by Ujjwal Kumar Gupta
There are different ways to close the app, depending on:
关闭应用程序的方法有多种,具体取决于:
if (navigator.app) {
navigator.app.exitApp();
} else if (navigator.device) {
navigator.device.exitApp();
} else {
window.close();
}
回答by Anil Singhania
navigator.app.exitApp();
add this line where you want you exit the application.
在您希望退出应用程序的位置添加此行。
回答by Shadow
@Pradip Kharbuja
@Pradip Kharbuja
In Cordova-2.6.0.js (l. 4032) :
在 Cordova-2.6.0.js (l. 4032) 中:
exitApp:function() {
console.log("Device.exitApp() is deprecated. Use App.exitApp().");
app.exitApp();
}
回答by Stevko
if(navigator.app){
navigator.app.exitApp();
}else if(navigator.device){
navigator.device.exitApp();
}
回答by Jamz D. Mozac
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
我确认不起作用。我使用phonegap 6.0.5 和cordova 6.2.0
回答by Helzgate
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
});
}