cordova 双击返回 退出程序

添加cordova插件

cordova plugin add cordova-plugin-x-toast

cordova prepare

实现双击返回退出程序代码

.run(function ($ionicPlatform, $rootScope, $location, $timeout, $ionicHistory) {
        $ionicPlatform.ready(function ($rootScope) {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            if (window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if (window.StatusBar) {
                // org.apache.cordova.statusbar required
                StatusBar.styleDefault();
            }
        });
        //双击退出
        $ionicPlatform.registerBackButtonAction(function (e) {
            //判断处于哪个页面时双击退出
            if ($location.path() == '/tab/dash') {
                if ($rootScope.backButtonPressedOnceToExit) {
                    ionic.Platform.exitApp();
                } else {
                    $rootScope.backButtonPressedOnceToExit = true;
                    window.plugins.toast.showWithOptions(
                        {
                            message: '再按一次退出系统',
                            duration: "short", // which is 2000 ms. "long" is 4000. Or specify the nr of ms yourself.
                            position: "bottom",
                            addPixelsY: -100,  // added a negative value to move it up a bit (default 0)
                                styling: {
                                    horizontalPadding: 20, // iOS default 16, Android default 50
                                    verticalPadding: 16 // iOS default 12, Android default 30
                                }
                            },
                            function(){}, // optional
                            function(){}    // optional
                        );
                    setTimeout(function () {
                        $rootScope.backButtonPressedOnceToExit = false;
                    }, 2000);
                }
            }
            else if ($ionicHistory.backView()) {
                $ionicHistory.goBack();
            } else {
                $rootScope.backButtonPressedOnceToExit = true;
                window.plugins.toast.showWithOptions(
                    {
                      message: '再按一次退出系统',
                      duration: "short", // which is 2000 ms. "long" is 4000. Or specify the nr of ms yourself.
                      position: "bottom",
                      addPixelsY: -100,  // added a negative value to move it up a bit (default 0)
                      styling: {
                          horizontalPadding: 20, // iOS default 16, Android default 50
                          verticalPadding: 16 // iOS default 12, Android default 30
                      }
                    },
                    function(){}, // optional
                    function(){}    // optional
                  );
                setTimeout(function () {
                    $rootScope.backButtonPressedOnceToExit = false;
                }, 2000);
            }
            e.preventDefault();
            return false;
        }, 101);
    })

 

posted @ 2016-06-15 16:53  听雨萧潇  阅读(1876)  评论(1编辑  收藏  举报