cordova插件实现手机返回键点击退出登陆问题

使用前首先要安装一个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);

 

即可实现想要的效果 

你可能感兴趣的:(cordova,android,监听手机返回键,cordova,返回键突出登录)