一、安裝插件
cordova plugin add cordova-plugin-x-toast
二、配置
將app.js中run函數(shù)改寫成如下“雙擊退出”以下代碼為新增改寫代碼,同時需要增加$rootScope,$location,$cordovaToast這兩個參數(shù)。
.run(function($ionicPlatform,$rootScope, $location,$cordovaToast) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
//雙擊退出
$ionicPlatform.registerBackButtonAction(function(e) {
//判斷處于哪個頁面時雙擊退出
var $thisPath = $location.path();
if ('/home' == $thisPath) {
if ($rootScope.backButtonPressedOnceToExit) {
ionic.Platform.exitApp();
} else {
$rootScope.backButtonPressedOnceToExit = true;
$cordovaToast.showShortCenter('再按一次退出系統(tǒng)');
setTimeout(function() {
$rootScope.backButtonPressedOnceToExit = false;
}, 2000);
}
} else {
history.go(-1);
}
e.preventDefault();
return false;
}, 101);
});
三、備注
經過上述配置,就已經可以了'/home' == $thisPath為在哪個頁面觸發(fā)效果。