美文网首页
WebView 写数据到 localStorage

WebView 写数据到 localStorage

作者: lixinxin | 来源:发表于2018-01-30 17:28 被阅读234次

设置

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        mWebView.getSettings().setAppCachePath(appCachePath);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        //mWebView.loadUrl("https://api.shanshanstory.com/local.html");
        mWebView.loadUrl("http://h5.inner.evente.cn:8000/form");

写数据

  @Override
        public void onPageFinished(WebView webView, String url) {
            super.onPageFinished(webView, url);
            mProgress.setVisibility(View.GONE);
            if (!isSetLocalStorage) {
                writeData(webView);
                webView.reload();
            }
            isSetLocalStorage = true;
        }


  public void writeData(WebView webView) {
        //String key = "requestcout";
        String key = "__EVENT__";
        String val = base64Json;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            webView.evaluateJavascript("window.localStorage.setItem('" + key + "','" + val + "');", null);
        } else {
            webView.loadUrl("javascript:localStorage.setItem('" + key + "','" + val + "');");
        }
    }

相关文章

网友评论

      本文标题:WebView 写数据到 localStorage

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