使用前首先要安裝一個cordova插件cordova-plugin-x-toast,然後調用下面這段代碼
var pageUrl = window.location.href;
var n = pageUrl.lastIndexOf('?');
var m = pageUrl.lastIndexOf('/');
var str = pageUrl.substring(m + 1, n); //獲取pageName
var exitAppTicker = 0;
document.addEventListener("deviceready", function() {
console.log("deviceready");
document.addEventListener("backbutton", function() {
if (str != 'modile_index.html' && str != 'login.html') {//顯示底部菜單按鈕的頁面
history.back();
} else if (str == 'modile_index.html' || str == 'login.html') {
if (exitAppTicker == 0) {
exitAppTicker++;
window.plugins.toast.showLongCenter('再按一次退出!');
setTimeout(function() {
exitAppTicker = 0;
}, 2000);
} else if (exitAppTicker == 1) {
navigator.app.exitApp(); //退出app
}
} else {
history.back();
}
}, false);
}, false);
即可實現想要的效果