简体   繁体   中英

How do I disable Google Adsense in an WebView Android App?

I have a website with Google Adsense ads. I want to integrate this website into an Android application using a Webview. However, I understand that Google does not allow their ads to be placed in any app including inside a webview. (They recommend Admob instead)

Therefore, what is the best way to detect that my website is in a WebView and disable the Adsense ad accordingly? I want to keep the adsense ads for my desktop users, but disable them altogether for my mobile app users. Any ideas? Thanks.

You can execute javascript to hide different views including ads while integrating your website in web view. Below is a sample code -

public class MainActivity extends AppCompatActivity {

    WebView webView;

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

        webView=(WebView) findViewById(R.id.web);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                String javaScript = "javascript:(function() { document.getElementById('google_image_div').remove();})()";

                webView.loadUrl(javaScript);    

            }
        });

        webView.loadUrl("YOUR_WEBSITE_URL");;

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM