WebView相關(guān)設置

  1. 重定向問題

通過setWebViewClient方法重寫WebViewClientEx內(nèi)部的shouldOverrideUrlLoading(WebView view, String url)方法控制頁面跳轉(zhuǎn)署尤。

WebView.HitTestResult hitTestResult = view.getHitTestResult();
通過hitTestResult.getType()判斷重定向類型签则,類型如下欢策;
網(wǎng)頁首次進入自動重定向 getType = 0

/**
 * Default HitTestResult, where the target is unknown.
 */
public static final int UNKNOWN_TYPE = 0;
/**
 * @deprecated This type is no longer used.
 */
@Deprecated
public static final int ANCHOR_TYPE = 1;
/**
 * HitTestResult for hitting a phone number.
 */
public static final int PHONE_TYPE = 2;
/**
 * HitTestResult for hitting a map address.
 */
public static final int GEO_TYPE = 3;
/**
 * HitTestResult for hitting an email address.
 */
public static final int EMAIL_TYPE = 4;
/**
 * HitTestResult for hitting an HTML::img tag.
 */
public static final int IMAGE_TYPE = 5;
/**
 * @deprecated This type is no longer used.
 */
@Deprecated
public static final int IMAGE_ANCHOR_TYPE = 6;
/**
 * HitTestResult for hitting a HTML::a tag with src=http.
*/
public static final int SRC_ANCHOR_TYPE = 7;
/**
 * HitTestResult for hitting a HTML::a tag with src=http +  HTML::img.
 */
public static final int SRC_IMAGE_ANCHOR_TYPE = 8;
/**
 * HitTestResult for hitting an edit text area.
 */
public static final int EDIT_TEXT_TYPE = 9;

2卦方。 webview常見的相關(guān)設置

// 調(diào)試WebView
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    if (0 != (BaseApplication.getInstance().getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
}

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(false);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setSavePassword(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setUseWideViewPort(true);
// 限制正文字體大小為100%挣柬,防止修改系統(tǒng)字體后造成的正文布局錯亂
webView.getSettings().setTextZoom(100); 
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
//無網(wǎng)時取本地緩存
if (!NetUtils.checkNetwork()) {
    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
} else {
    // load online by default
    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 
}
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setGeolocationEnabled(true);        webView.getSettings().setGeolocationDatabasePath(webView.getContext().getCacheDir().toString());       webView.getSettings().setUserAgentString(NetUtils.getUserAgent(true));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}

WebChromeClient webChromeClient = new WebChromeClientEx(webView) {
    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);
    }

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
                
    }

    @Override
    public void onHideCustomView() {
                
    }
};
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(new WebViewClientEx(webView) {

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        return super.shouldInterceptRequest(view, url);
    }

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        return super.shouldInterceptRequest(view, request);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view,url);
    }

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

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }

    @Override
    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
        super.onReceivedHttpAuthRequest(view, handler, host, realm);
    }

    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse){
        super.onReceivedHttpError(view, request, errorResponse);
    }
    
    @TargetApi(android.os.Build.VERSION_CODES.M)
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
    }
});
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末江解,一起剝皮案震驚了整個濱河市块仆,隨后出現(xiàn)的幾起案子贤牛,更是在濱河造成了極大的恐慌惋鹅,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件殉簸,死亡現(xiàn)場離奇詭異闰集,居然都是意外死亡沽讹,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門武鲁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來爽雄,“玉大人,你說我怎么就攤上這事沐鼠≈课粒” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵迟杂,是天一觀的道長刽沾。 經(jīng)常有香客問我,道長排拷,這世上最難降的妖魔是什么侧漓? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮监氢,結(jié)果婚禮上布蔗,老公的妹妹穿的比我還像新娘。我一直安慰自己浪腐,他們只是感情好纵揍,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著议街,像睡著了一般泽谨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上特漩,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天吧雹,我揣著相機與錄音,去河邊找鬼涂身。 笑死雄卷,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的蛤售。 我是一名探鬼主播丁鹉,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼悴能!你這毒婦竟也來了揣钦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤搜骡,失蹤者是張志新(化名)和其女友劉穎拂盯,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體记靡,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡谈竿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年团驱,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片空凸。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡嚎花,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出呀洲,到底是詐尸還是另有隱情紊选,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布道逗,位于F島的核電站兵罢,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏滓窍。R本人自食惡果不足惜卖词,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望吏夯。 院中可真熱鬧此蜈,春花似錦、人聲如沸噪生。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽跺嗽。三九已至战授,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間桨嫁,已是汗流浹背陈醒。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瞧甩,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓弥鹦,卻偏偏與公主長得像肚逸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子彬坏,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容