Android webView shouldInterceptRequest - 停止加载资源

2 人关注

我试图拦截并停止加载 webView 上的所有mp4视频,为此我使用了 shouldInterceptRequest 。我能够拦截视频,例如:显示一个对话框,但是,我不能停止加载它们,有什么想法吗?

webView.setWebViewClient(new WebViewClient() {
    @SuppressWarnings("deprecation")
    @Override
    public WebResourceResponse shouldInterceptRequest(final WebView view, String url) {
        try { 
            if (url.contains(".mp4?")) {
                return getResponseData();
        } catch (Exception e) {
            e.printStackTrace();
        return super.shouldInterceptRequest(view, url);
    private WebResourceResponse getResponseData() {
        try {
            String str = "Access Denied";
            InputStream data = new ByteArrayInputStream(str.getBytes("UTF-8"));
            return new WebResourceResponse("text/css", "UTF-8", data);
        } catch (IOException e) {
            return null;
    
2 个评论
你确定你的 getResponseData 方法没有返回空吗?这将导致视频继续加载。你是否尝试过用你的IDE调试这个应用程序,看看它到底在做什么?
if(url.contain(".mp4?)) 后面放一个日志。为什么你在 .mp4 之后有 ? ?如果日志不显示,就用 if(url.endsWith(".mp4")
java
android
webview
android-webview
Pedro Lobito
Pedro Lobito
发布于 2015-09-24
4 个回答
shyam
shyam
发布于 2015-09-24
0 人赞同

这可能有助于你。

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    if (yourCondition) {
        Handler handler = new Handler(getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                YourwebView.stopLoading();
                YourwebView.doAnythingYouWant();
    return super.shouldInterceptRequest(view, request);

我创建这个主题是因为A WebView method ( that is stopLoading() ) can't be called on a currently running thread and that thread is 'Chrome_FileThread'

Also, get getMainLooper() because In above situation we can't create handler inside thread that has not called Looper.prepare().

Hope this helps.

Ashwin
Ashwin
发布于 2015-09-24
0 人赞同

如果你的HTML页面不显示mp4内容或处理无效的内容,那么你可以简单地传递一些假的InputStream给WebResourceResponse,如下所示。

my_webview.webViewClient = object : WebViewClient() {
    override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
        if (request?.url?.contains(".mp4") == true) {
            val inputStream = "".byteInputStream(Charset.defaultCharset())
            return WebResourceResponse("image/png", "UTF-8", inputStream)
        } else {
            return super.shouldInterceptRequest(view, request)
    
Youssef Af
Youssef Af
发布于 2015-09-24
0 人赞同

Try this

public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                if (yourCondition) {
                    webView.post(new Runnable() {
                        @Override
                        public void run() {
                            webView.stopLoading();
                return null;
    
如果有更多的支持信息,你的答案可以得到改善。请 edit 添加进一步的细节,如引证或文件,以便其他人能够确认你的答案是正确的。你可以找到更多关于如何写好答案的信息 in the help center .
Mark Gilchrist
Mark Gilchrist
发布于 2015-09-24
0 人赞同

你想要的方法是这个,我想

webView.setWebViewClient(new WebViewClient(){
  @Override