Disable Android WebView/WebViewClient Initiated favicon.ico Request

后端 未结 5 852
独厮守ぢ
独厮守ぢ 2021-01-11 16:27

How can I disable the Android WebView/WebViewClient from sending out a request for favicon.ico when I call WebView.loadUrl()? I can see the call being made while profiling r

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-11 17:30

    for me the complete solution was:

       @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    
            if(url.toLowerCase().contains("/favicon.ico")) {
                try {
                    return new WebResourceResponse("image/png", null, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            return null;
        }
    
        @Override
        @SuppressLint("NewApi")
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    
            if(!request.isForMainFrame() && request.getUrl().getPath().endsWith("/favicon.ico")) {
                try {
                    return new WebResourceResponse("image/png", null, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            return null;
        }
    

提交回复
热议问题