.run(function ($ionicPlatform, $location, $ionicPopup, $ionicHistory, $rootScope, loginService, $state, $timeout) {
ionic.Platform.ready(function () {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)if(),
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
var again = false; //判断是不是第二次
// 绑定被硬件被点击的时候的事件
$ionicPlatform.registerBackButtonAction(function (e) {
//阻止默认的行为
e.preventDefault();
// 两次退出的时候使用的退出框
function showExitAlert() {
// 自定义弹窗
var myPopup = $ionicPopup.show({
title: '再点击一次退出应用',
});
// 成功弹出,设置again
again = true;
myPopup.then(function (res) {
// 在弹窗被销毁的时候执行
console.log('Tapped!', res);
});
$timeout(function () {
again = false;
myPopup.close(); // 1秒后关闭弹窗
}, 1000);
};
if (again) {
ionic.Platform.exitApp();
}
// 判断当前路由是否为各个导航栏的首页,是的话则显示提示框
if ($location.path() == '/tab/home' || $location.path() == '/tab/CRM' || $location.path() == '/tab/work' || $location.path() == '/tab/user-center' || $location.path() == '/tab/cooperate'||$location.path() == '/tab/cooperate') {
// showConfirm();
showExitAlert();
} else if ($ionicHistory.backView()) {
$ionicHistory.goBack();
} else {
// showConfirm();
showExitAlert();
}
return false;
}, 501); //101优先级常用于覆盖‘返回上一个页面'的默认行为
})
这里最关键的在于监听到点击硬件返回按钮这个事件。然后我们再进行处理
如果是第二次点击就退出而如果是第一次点击就进行弹窗,并且设置again为true,而如果在1秒内没有第二次点击就认为过时了,继续设置agian为false
注意点还有$ionicPopup.show()返回的是一个promise对象。他的第一个函数是在销毁弹窗的时候才会执行的。