简体   繁体   English

如何在 WebView Android 应用程序中禁用 Google Adsense?

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

I have a website with Google Adsense ads.我有一个带有 Google Adsense 广告的网站。 I want to integrate this website into an Android application using a Webview.我想使用 Webview 将此网站集成到 Android 应用程序中。 However, I understand that Google does not allow their ads to be placed in any app including inside a webview.但是,我知道 Google 不允许将他们的广告放置在任何应用程序中,包括在网络视图中。 (They recommend Admob instead) (他们推荐 Admob)

Therefore, what is the best way to detect that my website is in a WebView and disable the Adsense ad accordingly?因此,检测我的网站是否在 WebView 中并相应地禁用 Adsense 广告的最佳方法是什么? I want to keep the adsense ads for my desktop users, but disable them altogether for my mobile app users.我想为我的桌面用户保留 Adsense 广告,但为我的移动应用程序用户完全禁用它们。 Any ideas?有任何想法吗? Thanks.谢谢。

You can execute javascript to hide different views including ads while integrating your website in web view.您可以执行 javascript 来隐藏包括广告在内的不同视图,同时将您的网站集成到 Web 视图中。 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");;

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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