webview全屏播放 清除缓存

公司今年要做和js交互,以前不懂这块,网上查了查感觉蛮有意思的,特此记录下。

        WebSettings websetting = webview.getSettings();
       //启用js方法
        websetting.setJavaScriptEnabled(true);
       //启用或禁用DOM缓存。web端的Session Storage和Local Storage
        websetting.setDomStorageEnabled(true);
    @Override
    //设置回退
    //覆盖Activity类的onKeyDown
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (webview.canGoBack()) {
                webview.goBack(); //goBack()表示返回WebView的上一页面
                return true;
            } else {
                finish();
        }
     }
    return super.onKeyDown(keyCode, event);
    }

项目里有需要全屏视频播放的需求,web在Android端一直都不能全屏 我就试了试 结果可行。

//首先在xml上写一个FrameLayout
 <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:id="@+id/myProgressBar"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        />
    <WebView
        android:id="@+id/web"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <FrameLayout
        android:id="@+id/videoContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

webview.setWebChromeClient(new CustomWebViewChromeClient());

private class CustomWebViewChromeClient extends WebChromeClient {
//重写js的console看log
        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            CLog.i("JS+++", consoleMessage.message().toString());
            return super.onConsoleMessage(consoleMessage);
        }
//网页中有H5播放flash video的时候按下全屏按钮将会调用到这个方法,一般用作设置网页播放全屏操作
        @Override
        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
            fullScreen();
            webview.setVisibility(View.GONE);
            mVideoContainer.setVisibility(View.VISIBLE);
            webRelativeLayout.setVisibility(View.GONE);//导航栏可不记
            mVideoContainer.addView(view);
            mCallBack = callback;
            super.onShowCustomView(view, callback);
        }
//取消全屏方法
        @Override
        public void onHideCustomView() {
            fullScreen();
            if (mCallBack != null) {
                mCallBack.onCustomViewHidden();
            }
            webview.setVisibility(View.VISIBLE);
            webRelativeLayout.setVisibility(View.VISIBLE);//导航栏可不记
            mVideoContainer.removeAllViews();
            mVideoContainer.setVisibility(View.GONE);
            super.onHideCustomView();
        }
//应用程序当前网页加载的进度
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                bar.setVisibility(View.GONE);
            } else {
                bar.setVisibility(View.VISIBLE);
                bar.setProgress(newProgress);
            }
        }
//设置全屏横向纵向
 private void fullScreen() {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

接下来写监听全屏点击事件
webview.addJavascriptInterface(new JsObject(), "onClick");

private class JsObject {

        @JavascriptInterface
        public void fullscreen() {
            //监听到用户点击全屏按钮
            fullscreen();
        }
    }

这就完成了。
项目里有清除cookie和localstorage的需求,这个清除localstorage我没有用网上大部分给的一层一层删除/data/data/packname的方法做 ,我试过那个方法 不管用不知道为什么,我换了一种思路直接执行JS代码,因为localstorage是键值对形式存在的,我就直接把对应的key值给改变了 结果可行。
webview.evaluateJavascript("window.localStorage.setItem('" + "loginInfo" + "','" + "我填的空" + "');", null);
对于清除cookie我用的网上的方法 不知道好不好使 看不到效果 ,这里也附上代码:

        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);     
        cookieManager.removeSessionCookie();
        cookieManager.removeAllCookie();
        cookieSyncMngr.sync();

最后清除webview数据等 , 完事!

 @Override
    protected void onDestroy() {
        super.onDestroy();
        webview.clearCache(true);
        getCacheDir().delete();//不知道好不好使 我怀疑web在存的时候没有存到特定目录下
        webview.clearFormData();
        webview.clearSslPreferences();
        webview.clearHistory();
        webview.removeAllViews();
        webview.destroy();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在学习WebView的时候就知道了WebView会出现很多稀奇古怪的问题,真碰上的时候还是焦头烂额,很多问题的解决...
    _戏_梦阅读 5,880评论 2 35
  • 前言 关于UIWebView的介绍,相信看过上文的小伙伴们,已经大概清楚了吧,如果有问题,欢迎提问。 本文是本系列...
    CoderLF阅读 9,010评论 2 12
  • 这篇博客主要来介绍 WebView 的相关使用方法,常见的几个漏洞,开发中可能遇到的坑和最后解决相应漏洞的源码,以...
    Shawn_Dut阅读 7,283评论 3 55
  • -01- “毕竟,先走的是比较幸福的,留下来的,也并不是强者,可是,在这彻心的苦,切肤的疼痛里,我仍是要说——‘为...
    鹅卵石妈妈阅读 499评论 8 7
  • 刚刚过去的2016年被称为仅次于1998年长江流域特大洪水的一年,今年防汛形势亦不容乐观。由于洞庭湖水系连续强降雨...
    地中海的传说阅读 128评论 0 0