美文网首页
WebView相关

WebView相关

作者: 案玉璃青 | 来源:发表于2018-09-03 10:40 被阅读0次

    2018-09-03

        第一次记录文章,有不对的地方,请见谅。

        WebView内存泄漏问题

    public void destroy() {

    if (mWebView !=null) {

    mWebView.loadUrl("about:blank");

            mWebView.removeAllViews();

            releaseConfigCallback();

            mWebView.destroy();

            mWebView =null;

        }

    }

    private void releaseConfigCallback() {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {// JELLY_BEAN

            try {

    Field field = WebView.class.getDeclaredField("mWebViewCore");

                field = field.getType().getDeclaredField("mBrowserFrame");

                field = field.getType().getDeclaredField("sConfigCallback");

                field.setAccessible(true);

                field.set(null, null);

            }catch (NoSuchFieldException e) {

    e.printStackTrace();

            }catch (IllegalAccessException e) {

    e.printStackTrace();

            }

    }else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {// KITKAT

            try {

    Field sConfigCallback = Class.forName("android.webkit.BrowserFrame").getDeclaredField("sConfigCallback");

                if (sConfigCallback !=null) {

    sConfigCallback.setAccessible(true);

                    sConfigCallback.set(null, null);

                }

    }catch (NoSuchFieldException e) {

    e.printStackTrace();

            }catch (ClassNotFoundException e) {

    e.printStackTrace();

            }catch (IllegalAccessException e) {

    e.printStackTrace();

            }

    }

    }

    WebView初始化准备

    public ReveeWeb prepare(Context context) {

    mWebView.setVerticalScrollBarEnabled(false);

            mWebView.setAlwaysDrawnWithCacheEnabled(true);

            mWebView.setAnimationCacheEnabled(true);

            mWebView.setDrawingCacheBackgroundColor(Color.WHITE);

            mWebView.setDrawingCacheEnabled(true);

            mWebView.setWillNotCacheDrawing(false);

            mWebView.setSaveEnabled(true);

            mWebView.setFocusable(true);

            mWebView.setFocusableInTouchMode(true);

            WebSettings settings = mWebView.getSettings();

            settings.setTextZoom(100);

            settings.setJavaScriptEnabled(true);

            settings.setDomStorageEnabled(true);

            settings.setSupportZoom(true);

            settings.setBuiltInZoomControls(true);

            settings.setDisplayZoomControls(false);

            settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

            settings.setUseWideViewPort(true);

            settings.setLoadWithOverviewMode(true);

            settings.setPluginState(WebSettings.PluginState.ON_DEMAND);

    //        settings.setMediaPlaybackRequiresUserGesture(false);

            // 图片延迟加载,加快web的加载速度

            settings.setLoadsImagesAutomatically(Build.VERSION.SDK_INT >=19);

            // 根据网络状态改变缓存策略

            if (isNetworkConnected(context)) {

    settings.setCacheMode(WebSettings.LOAD_DEFAULT);

            }else {

    settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

            }

    settings.setAppCacheEnabled(true);

            settings.setAppCachePath(context.getCacheDir().toString());

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

    settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

            }

    mWebView.setWebViewClient(new ReveeWebViewClient());

    return this;

        }

    Android和JS的调用

    webView.loadUrl("javascript:(function () {\n" +

    "    var btn = document.getElementsByClassName('x-video-button')[0];\n" +

    "    if (btn != null) {\n" +

    "        btn.click();\n" +

    "    }\n" +

    "    var video = document.getElementsByTagName('video')[0];\n" +

    "    video.volume = 0;\n" +

    "    video.addEventListener('timeupdate', function () {\n" +

    "        window.android.onTimeUpdate(this.currentTime);\n" +

    "    });\n" +

    "    video.addEventListener('ended', function () {\n" +

    "        window.android.onEnd();\n" +

    "    });\n" +

    "\n" +

    "    video.play();\n" +

    "})()");

    /**

    * js回调接口

    */

    private class WebViewCallback {

    private double currentTime;

        /**

        * 监听video标签的timeupdate,如果播放时长超过了15秒,证明播放两个广告完成了,已经进入正片,表示播放成功

        *

        * @param time

        */

        @JavascriptInterface

        public void onTimeUpdate(double time) {

    currentTime = time;

            Log.e("guoxing", "time:" + time);

            // 时间大于0证明已经开始播放广告了,视频播放任务调起成功

            if (time >0 && !playSuccess) {

    playSuccess =true;

                Log.e("guoxing", "playSuccess========================");

                if (listener !=null) {

    listener.onPlaySuccess();

                }

    }

    // 时间大于指定的时长,说明任务完成了,但是视频可能还处于播放状态

            if (time >mDuration && !onDuration) {

    onDuration =true;

                isPlay =true;

                Log.e("guoxing", "onDuration======================");

                if (listener !=null) {

    listener.onDuration();

                }

    new Handler(Looper.getMainLooper()).post(new Runnable() {

    @Override

                    public void run() {

    release();

                    }

    });

            }

    }

    /**

        * 监听video标签的ended回调,表示视频播放完成

        */

        @JavascriptInterface

        public void onEnd() {

    Log.e("guoxing", "onEnd=========================");

            if (currentTime >mDuration &&listener !=null) {

    listener.onEnd();

            }

    }

    }

    拿到Cookie

    CookieManager cookieManager = CookieManager.getInstance();

    String CookieStr = cookieManager.getCookie(url);

    以后再补充。。

    相关文章

      网友评论

          本文标题:WebView相关

          本文链接:https://www.haomeiwen.com/subject/ackbwftx.html