phonegap 退出确认

实现 再按一次退出  ,这里只针对 主active继承 DroidGap 或者CordovaActive

以下有2种

方案1: 重写CordovaWebView类

新建类NobackWebView

package com.example.xiaoyang;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.cordova.CordovaWebView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.Toast;

public class NobackWebView  extends CordovaWebView{
    public NobackWebView(Context context) {
        super(context);
        }
        public NobackWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        }
        public NobackWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        }
        public NobackWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
        super(context, attrs, defStyle, privateBrowsing);
        }
        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        exitBy2Click();
        return true;
        }else{
        return super.onKeyUp(keyCode, event);
        }
        }
        public void toastMessage(String msg, int duration) {
        Toast.makeText(this.getContext(), msg, duration).show();
        }
        private static Boolean isExit = false;
        private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
        isExit = true; // 准备退出  
        toastMessage("再按一次退出程序", 2000);
        tExit = new Timer();
        tExit.schedule(new TimerTask() {
        @Override
        public void run() {
        isExit = false; // 取消退出  
                        }
        }, 2000); // 如果2秒钟内没有按下返回键,则启动定时器取消掉刚才执行的任务  
        } else {
        System.exit(0);
        }
        }
}

 然后在主active 中init

    @Override
    public void init() {
    //只是把源码中的CordovaWebView换成NobackWebView,其他还是源码
    CordovaWebView webView = new NobackWebView(this);
    CordovaWebViewClient webViewClient;
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
    webViewClient = new CordovaWebViewClient(this, webView);
    } else {
    webViewClient = new IceCreamCordovaWebViewClient(this, webView);
    }
    this.init(webView, webViewClient,
    new CordovaChromeClient(this, webView));
    }

效果: 在任意界面按退出都会 弹出"再按一次退出"的dialog

方案2

在首页 js中注册

script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.2/jquery.mobile.min.js"></script>
 <script type="text/javascript" src="js/cordova.js"></script>
       <script type="text/javascript" src="js/cordova_plugins.js"></script>
  
   <script>
   document.addEventListener("deviceready", onDeviceReady, false);
            function onDeviceReady() {
            //alert("已经弄完了")
            s = setInterval(function(){
            //销毁启动动画
                navigator.splashscreen.hide();
                },2000);
             //注册返回事件
             document.addEventListener("backbutton",onBackKeyDown,false);
            };
        //BackButton按钮
        function onBackKeyDown(){
            if($.mobile.activePage.is('#pageone')){
                  navigator.notification.confirm(
            '按确定退出程序!',  // message
            onConfirm,     // callback to invoke with index of button pressed
            '确定要退出程序吗?',            // title
            '确定,取消'          // buttonLabels
        );
            }
            else {
                navigator.app.backHistory();
            }
        }
         function onConfirm(button) {
        //alert('You selected button ' + button);
  if(button==1) navigator.app.exitApp(); //选择了确定才执行退出

    }
            
        
   </script>

效果:以弹出框的形式,并且只在首页时,按退出时进行提示,其它页面只是返回上一页

原文地址:https://www.cnblogs.com/sheapchen/p/4040403.html