Android 在 webview 中阻止广告 [英] Android block ads in webview

查看:56
本文介绍了Android 在 webview 中阻止广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在自定义 webview 客户端(没有 JavaScript 注入)中实现一种可以阻止广告的机制.有没有一种方法可以捕获广告并将其替换为来自可信赖来源的其他广告?谢谢

I want to implement a mechanism in a custom webview client (without JavaScript injection) that can block ads. Is a way I can catch ads and replace them with other ads from a trusted source? Thanks

推荐答案

在你的自定义 WebViewClient,你可以覆盖函数 shouldInterceptRequest(WebView, WebResourceRequest).

In your custom WebViewClient, you can override the function shouldInterceptRequest(WebView, WebResourceRequest).

来自 Android 文档:

From Android docs:

将资源请求通知宿主应用程序并允许应用程序返回数据.

Notify the host application of a resource request and allow the application to return the data.

所以一般的想法是检查请求是否来自广告网址(大量黑名单过滤掉了那里),然后返回一个不是广告的假"资源.

So the general idea is to check if the request is coming from an ad URL (plenty of black list filters out there), then return a "fake" resource that isn't the ad.

要获得更深入的解释和示例,我建议查看此 博文.

For a more in depth explanation plus an example, I recommend checking out this blog post.

这篇关于Android 在 webview 中阻止广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆