Webview localStorage.setItem(javascript)无法在加载url之前设置(android)

时间:2016-08-23 15:13:37

标签: javascript android webview

在加载我的网址之前,我需要设置localStorage.setItem

但是,当我第一次加载URL时,没有任何内容出现。如果我第二次刷新页面,则会出现。如何在第一次加载页面时设置localStorage.setItem

webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);

        //set token and role
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                webView.loadUrl(
                        "javascript:" +
                                "localStorage.setItem('token', '" + token + "');");
            }

        });


        webView.setWebChromeClient(new WebChromeClient() {
            private ProgressDialog mProgress;

            @Override
            public void onProgressChanged(WebView view, int progress) {
                try {
                    if (mProgress == null) {
                        mProgress = new ProgressDialog(getActivity());
                        if (!getActivity().isFinishing()) {
                            mProgress.show();
                        }
                    }

                    mProgress.setMessage(getString(R.string.loading) + String.valueOf(progress) + "%");
                    if (progress == 100) {
                        mProgress.dismiss();
                        mProgress = null;
                    }

                } catch (Exception e) {
                    Log.e("item menu", e.toString());
                }
            }
        });



        webView.loadUrl(url);

1 个答案:

答案 0 :(得分:0)

正如你所说,本地存储数据不是第一次出现......我遇到了同样的问题......这是解决方案。 将 localstorage.setItem(key, val) 放在 onPageStarted() 和 onPageFinished() 方法中。

公共类 AppWebViewClients 扩展 WebViewClient { private ProgressBar progressBar;

    public AppWebViewClients(ProgressBar progressBar) {
        this.progressBar=progressBar;
        progressBar.setVisibility(View.VISIBLE);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webview.evaluateJavascript("window.localStorage.setItem('"+ key1 +"','"+ val1 +"');", null);
            webview.evaluateJavascript("window.localStorage.setItem('"+ key2 +"','"+ val2 +"');", null);

        } else {
            webview.loadUrl("javascript:localStorage.setItem('"+ key1 +"','"+ val1 +"');");
            webview.loadUrl("javascript:localStorage.setItem('"+ key2 +"','"+ val2 +"');");

            }

        }

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webview.evaluateJavascript("window.localStorage.setItem('"+ key1 +"','"+ val1 +"');", null);
            webview.evaluateJavascript("window.localStorage.setItem('"+ key2 +"','"+ val2 +"');", null);

        } else {
            webview.loadUrl("javascript:localStorage.setItem('"+ key1 +"','"+ val1 +"');");
            webview.loadUrl("javascript:localStorage.setItem('"+ key2 +"','"+ val2 +"');");

            }

        }
    }
}

希望它能解决您的问题。 谢谢