Android 安卓WebView套壳H5网页 手机返回键问题(过滤二级页面,返回键相应给WebView)

1.首先 重写 onKeyDown方法  添加如下代码:

/**
 * 按键响应,在WebView中查看网页时,检查是否有可以前进的历史记录。
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {

        // 返回键退回
        webView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up
    // to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

2.代码写到这里会出现一个问题,多次点击底部导航按钮 再点击返回键 会相应WebView的GoBack,导致在首页 底部导航反向跳而不退出应用 

解决办法,

   a.  声明成员变量 :

   private boolean isMainAvtivity = true;
b.onKeyDown方法中 改写成如下 意思是如果
isMainAvtivity是true的话 出走退出应用的代码 如果是false的话 相应webview的GoBack事件
/**
 * 按键响应,在WebView中查看网页时,检查是否有可以前进的历史记录。
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if (isMainAvtivity) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();

                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return true;
    }
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {

        // 返回键退回
        webView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up
    // to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}
3.
isMainAvtivity 的值 是通过过滤网页进行true或者false的赋值
代码如下:
    // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!TextUtils.isEmpty(url)) {
                    videoFlag = url.contains("vedio");
                }
                if (url.trim().startsWith("tel")) {//特殊情况tel,调用系统的拨号软件拨号【1111111111                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                } else {
                    String port = url.substring(url.lastIndexOf(":") + 1, url.lastIndexOf("/"));//尝试要拦截的视频通讯url格式(808端口):【http://xxxx:808/?roomName                    if (port.equals("808")) {//特殊情况【若打开的链接是视频通讯地址格式则调用系统浏览器打开】
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                    } else {//其它非特殊情况全部放行
                        view.loadUrl(url);
                    }
                }
                return true;

            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.e("========", "66666 ");
                //重新onPageStarted方法 如果加载的是一级页面 isMainActivity==true,
                if ("http://..../index.php?a=/home/user/shouye.html".equals(url)) {
                    isMainAvtivity = true;
                }
                if ("http://...../index.php?a=/home/shop/index.html".equals(url)) {
                    isMainAvtivity = true;
                }
                if ("http://...../index.php?a=/home/user/zhuanpan.html".equals(url)) {
                    isMainAvtivity = true;
                }
                if ("http://...../index.php?a=/home/user/index.html".equals(url)) {
                    isMainAvtivity = true;
                }

                //如果是从一级页面跳转到二级的页面 通通给于false,可以用contains进行判断
                if (url != null) {
                    if (url.contains("http://...../index.php?a=/home/shop/xiangqing/id/")) {
                        isMainAvtivity = false;
                    }
                }
                if (url != null) {
                    if (url.contains("http://...../index.php?a=/home/shop/jiesuan/id/")) {
                        isMainAvtivity = false;
                    }
                }


                if ("http://...../index.php?a=/home/user/shengji.html".equals(url)) {
                    isMainAvtivity = false;
                }
                if ("http://...../index.php?a=/home/user/updatemembermsg.html".equals(url)) {
                    isMainAvtivity = false;
                }
                if ("http://...../index.php?a=/home/user/zjgl.html".equals(url)) {
                    isMainAvtivity = false;
                }
                if ("http://...../index.php?a=/home/user/dingdan.html".equals(url)) {
                    isMainAvtivity = false;
                }
                if ("http://...../index.php?a=/home/user/userlist.html".equals(url)) {
                    isMainAvtivity = false;
                }
                if ("http://...../index.php?a=/home/index/index.html".equals(url)) {
                    isMainAvtivity = false;
                }


                if ("http://...../index.php?a=/home/user/cz_record.html".equals(url)) {
                    isMainAvtivity = false;
                }

                if ("http://...../index.php?a=/home/user/chongzhi.html".equals(url)) {
                    isMainAvtivity = false;
                }

                if ("http://...../index.php?a=/home/user/tixian.html".equals(url)) {
                    isMainAvtivity = false;
                }

                if ("http://...../index.php?a=/home/user/tixian_record.html".equals(url)) {
                    isMainAvtivity = false;
                }
                if ("http://...../index.php?a=/home/bonus/financialflow.html".equals(url)) {
                    isMainAvtivity = false;
                }

                if ("http://...../index.php?a=/home/user/gonggao/param/1.html".equals(url)) {
                    isMainAvtivity = false;
                }

                if ("http://...../index.php?a=/home/user/gonggao/param/2.html".equals(url)) {
                    isMainAvtivity = false;
                }
                if ("http://...../index.php?a=/home/user/chongzhi".equals(url)) {
                    isMainAvtivity = false;
                }
            }


            @Override
            public void onPageFinished(WebView view, String url) {
                //页面加载完毕,结束对话框

            }

            @Override
            public void onLoadResource(WebView view, String url) {
                //加载页面资源,比如每加载一张图片就会调用
            }


            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                //页面加载错误回掉
                switch (errorCode) {
                    case 404:
//                        view.loadUrl("file:///android_assets/error_handle.html");
                        break;
                }
            }


        });
3.至此 大功告成



你可能感兴趣的:(android)