marcelbohland / Android-Webview-Adblock

A Adblock System for Android Webview

Home Page:https://github.com/marcelbohland/Android-Webview-Adblock-Example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ad comes back when the webpage url is changed

willjones911 opened this issue · comments

hey thanks for the help with ur code and other tutorials from codebit25 on youtube i kinda made the webview app work but the ad comes back when a change in url is detected then causing redirect and by pressing return it exits the app but after searching online
i've come across a post https://stackoverflow.com/questions/9312499/android-detect-webview-url-change/56395424#56395424

where it mentions
override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) {
// your code here
super.doUpdateVisitedHistory(view, url, isReload)
}

by adding the above code it can fix this issue can u help me on where do i add the above code?

my MainActivity.java is as below, i'm assuming there's where i add it, your help will be highly appreciated i'm a newbie to this

package com.example.ngefilm21;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AdBlocker.init(this);

    webview=(WebView)findViewById(R.id.webview);
    webview.setWebViewClient(new MyBrowser());

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webview.loadUrl("https://www.ngefilm21.asia/"); //type your url
}

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    private Map<String, Boolean> loadedUrls = new HashMap<>();
    @Nullable
    @Override

    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        boolean ad;
        if (!loadedUrls.containsKey(url)) {
            ad = AdBlocker.isAd(url);
            loadedUrls.put(url, ad);
        } else {
            ad = loadedUrls.get(url);
        }
        return ad ? AdBlocker.createEmptyResource() :
                super.shouldInterceptRequest(view, url);
    }
}

}

commented

@willjones911 have you gotten it to work yet cause I too have trouble with the ad blocking but with youtube video ads? btw

override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) 
{
       // your code here
       super.doUpdateVisitedHistory(view, url, isReload)
}

for Java should be this (below) the top code is for Kotlin if i'm not wrong?

public void doUpdateVisitedHistory(WebView view, String url, boolean isReload)
{
       // your code here
       super.doUpdateVisitedHistory(view, url, isReload);
}

If you have found a solution, please do let me know how you overcome it. Thanks