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);
    })

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM