ionic android双击退出应用和物理返回按钮隐藏键盘的实现

angular.module('starter', ['ionic', 'route', 'config', 'global', 'commonJs', 'ngCordova'])
    .run(['$ionicPlatform', '$rootScope', '$location', '$timeout', '$ionicHistory', 'CommonJs', '$cordovaToast','$cordovaKeyboard', function ($ionicPlatform, $rootScope, $location, $timeout, $ionicHistory,CommonJs,$cordovaToast,$cordovaKeyboard) {
        $ionicPlatform.ready(function () {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if (window.StatusBar) {
                // org.apache.cordova.statusbar required
                StatusBar.styleLightContent();
            }
        });
 
        //物理返回按钮控制&双击退出应用
        $ionicPlatform.registerBackButtonAction(function (e) {
            //判断处于哪个页面时双击退出
            if ($location.path() == '/tab/todoList' || $location.path() == '/tab/doneList' || $location.path() == '/tab/account'|| $location.path() == '/login') {
                if ($rootScope.backButtonPressedOnceToExit) {
                    ionic.Platform.exitApp();
                } else {
                    $rootScope.backButtonPressedOnceToExit = true;
                    $cordovaToast.showShortBottom('再按一次退出系统');
                    setTimeout(function () {
                        $rootScope.backButtonPressedOnceToExit = false;
                    }, 2000);
                }
            }else if ($ionicHistory.backView()) {
                if ($cordovaKeyboard.isVisible()) {
                    $cordovaKeyboard.close();
                } else {
                    $ionicHistory.goBack();
                }
            }
            else {
                $rootScope.backButtonPressedOnceToExit = true;
                $cordovaToast.showShortBottom('再按一次退出系统');
                setTimeout(function () {
                    $rootScope.backButtonPressedOnceToExit = false;
                }, 2000);
            }
            e.preventDefault();
            return false;
        }, 101);
 
    }]);

 转自:http://my.oschina.net/u/1416844/blog/490097

你可能感兴趣的:(ionic android双击退出应用和物理返回按钮隐藏键盘的实现)