android webview(AgentWeb) 刷新無效問題

問題描述:我的h5鏈接中有#號,比如:url="home/#/?token=123"
就是因為這個#號導(dǎo)致頁面刷新不好用了。
解決方案(總體思路):handler延遲刷新。
我用到的技術(shù):

1.AgentWeb(對安卓的WebView做了一些封裝,你也可以把它看成安卓的WebView。傳送門

2.handler和h5刷新相關(guān)。

如果你用的是安卓的WebView,你看完解決方案(思路)后,可以直接去看第6條。

下面是代碼
1.全局變量

private Handler dataHandler;
private AgentWeb mAgentWeb;
private String url = "https://www.baidu.com/";

2.在onCreate()方法中創(chuàng)建handler和AgentWeb;里面的binding.linWeb就是xml布局文件中的lin_web

//創(chuàng)建handler
dataHandler = new Handler(Looper.getMainLooper());

mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(binding.linWeb, new LinearLayout.LayoutParams(-1, -1))
                .closeIndicator()//使用默認(rèn)進(jìn)度條
                .setWebViewClient(new WebViewClient())
                .createAgentWeb()
                .ready()
                .go(url);

3.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/lin_web"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" />

</LinearLayout>

4刷新方法

/**
     * 刷新頁面
     */
    public void reloadPage() {
        if (mAgentWeb == null) return;
        //拿到這個頁面,最后一次加載的url
        String lastLoadUrl = mAgentWeb.getWebCreator().getWebView().getUrl();
        //停止上次刷新
        mAgentWeb.getUrlLoader().stopLoading();
        AgentWebConfig.removeAllCookies();
        AgentWebConfig.clearDiskCache(this.getContext());
        mAgentWeb.getUrlLoader().loadUrl(url);
        if (dataHandler != null && lastLoadUrl != null && lastLoadUrl.equals(url)) {
            dataHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mAgentWeb.getWebCreator().getWebView().reload();
                }
            }, 150);
        }
}

5.如果你的刷新還有問題,那可能和h5的寫法有關(guān),用用下面這種刷新方法,我們項目里h5我都是用了2種刷新方法才解決,坑啊。

//刷新頁面
    public void reloadPage() {
        if (mAgentWeb == null) return;
        //拿到這個頁面,最后一次加載的url
        String lastLoadUrl = mAgentWeb.getWebCreator().getWebView().getUrl();
        mAgentWeb.getUrlLoader().stopLoading();
        AgentWebConfig.removeAllCookies();
        AgentWebConfig.clearDiskCache(this.getContext());

        if (handler != null) {
            if (lastLoadUrl != null && lastLoadUrl.equals(url)) {
                mAgentWeb.getWebCreator().getWebView().reload();
            } else {
                mAgentWeb.getUrlLoader().loadUrl(url);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mAgentWeb.getWebCreator().getWebView().reload();
                    }
                }, 150);

            }
        }
    }

好了,上面就是AgentWeb的所有內(nèi)容了,下面的和安卓自帶的WebView相關(guān)。

6.安卓自帶的(WebView)
首先說明安卓自帶的webview我沒有嘗試,但是原理和思路是一樣的,嘗試的成本也不大。下面的是偽代碼:

public void reloadPage() {
        if (webview == null) return;
        //拿到這個頁面,最后一次加載的url
        String lastLoadUrl = webview.getUrl();
//TODO 注意如果你有緩存,注意這里要清一下緩存
        webview.loadUrl(url);
        if (dataHandler != null && lastLoadUrl != null && lastLoadUrl.equals(url)) {
            dataHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    webview.reload();
                }
            }, 150);
        }
}

//注意,或者是下面的刷新方法,2選1即可


//刷新頁面
    public void reloadPage2() {
        if (webview == null) return;
        //拿到這個頁面,最后一次加載的url
        String lastLoadUrl = webview.getUrl();
//TODO 注意如果你有緩存,注意這里要清一下緩存
        if (handler != null) {
            if (lastLoadUrl != null && lastLoadUrl.equals(url)) {
                webview.reload();
            } else {
                webview.loadUrl(url);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        webview.reload();
                    }
                }, 150);
            }
        }
    }

好了,如果對你有幫助可以幫忙點一個小紅心~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容