码迷,mamicode.com
首页 > 移动开发 > 详细

ionic双击退出键退出APP

时间:2015-05-10 12:45:17      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

1、安装插件

Toast is compatible with Cordova Plugman, compatible with PhoneGap 3.0 CLI, here‘s how it works with the CLI (backup your project first!):

Using the Cordova CLI and the Cordova Plugin Registry

$ cordova plugin add nl.x-services.plugins.toast
$ cordova prepare

Or using the phonegap CLI

$ phonegap local plugin add nl.x-services.plugins.toast

安装后可以用命令查看 http://www.cnblogs.com/cuoreqzt/p/4491943.html

 

2、在app中加上js

.run(function ($ionicPlatform, $rootScope, $location, $timeout, $ionicHistory) {
        $ionicPlatform.ready(function () {
            // 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 && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if (window.StatusBar) {
                // org.apache.cordova.statusbar required
                StatusBar.styleLightContent();
            }
            //双击退出
            if (window.cordova && window.plugins && window.cordova.plugins.Toast){
                $ionicPlatform.registerBackButtonAction(function (e) {
                    //判断处于哪个页面时双击退出
                    if ($location.path() == ‘/tab/home‘) {
                        if ($rootScope.backButtonPressedOnceToExit) {
                            ionic.Platform.exitApp();
                        } else {
                            $rootScope.backButtonPressedOnceToExit = true;
                            cordova.plugins.Toast.showShortTop(‘再按一次退出系统‘);
                            setTimeout(function () {
                                $rootScope.backButtonPressedOnceToExit = false;
                            }, 2000);
                        }
                    }
                    else if ($ionicHistory.backView()) {
                        $ionicHistory.goBack();
                    } else {
                        $rootScope.backButtonPressedOnceToExit = true;
                        cordova.plugins.Toast.showShortTop(‘再按一次退出系统‘);
                        setTimeout(function () {
                            $rootScope.backButtonPressedOnceToExit = false;
                        }, 2000);
                    }
                    e.preventDefault();
                    return false;
                }, 101);
            }

        });
    })

 

ionic双击退出键退出APP

标签:

原文地址:http://www.cnblogs.com/cuoreqzt/p/4491933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!