BÀI 77 - CHẶN QUẢNG CÁO TỪ WEBVIEW TRONG ANDROID

  • Trong bài trước mình đã chia sẻ cách tạo một WebView trong ứng dụng. Bài này mình sẽ chia sẻ cách chặn quảng cáo từ web đổ vào WebView của bạn.

  • Mã nguồn ở đây: https://github.com/MonsterTechnoGits/WebViewAdblock-Library

  • Và video hướng dẫn mình để ở cuối bài ->

  • Đầu tiên trong, gradle(projects) bạn thêm lớp này

allprojects {

repositories {

maven { url 'https://jitpack.io' }

}

}

  • Trong gradle(ứng dụng/app) bạn thêm thư viện này

dependencies {

implementation 'com.github.MonsterTechnoGits:WebViewAdblock:1.1'

}

  • Bạn tạo webview bình thường trong xml, trong java bạn sửa chút xíu như này.

  • Bạn thêm class chặn quảng cáo như dưới

private static class Browser_home extends android.webkit.WebViewClient {


Browser_home() {}


@SuppressWarnings("deprecation")

@Override

public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

return AdBlockerWebView.blockAds(view,url) ? AdBlocker.createEmptyResource() :

super.shouldInterceptRequest(view, url);

}

}

  • Cuối cùng, bạn setup cho webview. Chỉ khác bình thường ở chỗ hiển thị thôi

new AdBlockerWebView.init(this).initializeWebView(wvTinTuc);

wvTinTuc.setWebViewClient(new Browser_home());


wvTinTuc.loadUrl("url");


WebSettings webSettings = wvTinTuc.getSettings();

webSettings.setJavaScriptEnabled(true);

  • Video hướng dẫn