Configurar o WebView

如果您的 Android 应用使用了 WebView 来展示 Web 内容, 建议您对其进行配置,以便通过广告以最优方式从内容中获利。

本指南介绍如何提供有关如何配置 WebView 对象。

启用第三方 Cookie

为了提升用户的广告体验,并与 Chrome 的 Cookie 政策、启用第三方 您的 WebView 实例上的 Cookie。

Java

CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);

Kotlin

CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)

Web 设置

As configurações padrão de WebView não são otimizadas para anúncios. Use o WebSettings APIs para configurar o WebView para:

  • JavaScript
  • Acesso ao armazenamento local
  • Reprodução automática de vídeo

Java

import android.webkit.CookieManager;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
  private WebView webView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = findViewById(R.id.webview);

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
    // Let the web view use JavaScript.
    webView.getSettings().setJavaScriptEnabled(true);
    // Let the web view access local storage.
    webView.getSettings().setDomStorageEnabled(true);
    // Let HTML videos play automatically.
    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
  }
}

Kotlin

import android.webkit.CookieManager
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
  lateinit var webView: WebView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    webView = findViewById(R.id.webview)

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
    // Let the web view use JavaScript.
    webView.settings.javaScriptEnabled = true
    // Let the web view access local storage.
    webView.settings.domStorageEnabled = true
    // Let HTML videos play automatically.
    webView.settings.mediaPlaybackRequiresUserGesture = false
  }
}

加载网页视图内容

Cookie 和网页网址对于通过网页浏览创收非常重要, 与 基于网络的网址。为了获得更好的 WebView 广告效果, 直接从基于网络的网址加载网页内容。避免使用 WebViewAssetLoader,正在加载 或动态生成网页内容。

Java

import android.webkit.CookieManager;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
  private WebView webView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = findViewById(R.id.webview);

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
    // Let the web view use JavaScript.
    webView.getSettings().setJavaScriptEnabled(true);
    // Let the web view access local storage.
    webView.getSettings().setDomStorageEnabled(true);
    // Let HTML videos play automatically.
    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);

    // Load the URL for optimized web view performance.
    webView.loadUrl("https://webview-api-for-ads-test.glitch.me");
  }
}

Kotlin

import android.webkit.CookieManager
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
  lateinit var webView: WebView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    webView = findViewById(R.id.webview)

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
    // Let the web view use JavaScript.
    webView.settings.javaScriptEnabled = true
    // Let the web view access local storage.
    webView.settings.domStorageEnabled = true
    // Let HTML videos play automatically.
    webView.settings.mediaPlaybackRequiresUserGesture = false

    // Load the URL for optimized web view performance.
    webView.loadUrl("https://webview-api-for-ads-test.glitch.me")
  }
}

测试网页视图

在应用开发过程中,我们建议您加载以下测试网址:

https://webview-api-for-ads-test.glitch.me#webview-settings-tests

以验证这些设置对广告是否产生了预期效果。测试网址包含 符合完整集成的成功标准。

网站数据视图设置

  • 第三方 Cookie 的工作原理
  • 第一方 Cookie 的工作原理
  • 已启用 JavaScript
  • 已启用 DOM 存储

视频广告

  • 视频广告以内嵌方式播放,且无法在内置模式下打开 球员
  • 视频广告无需点击播放按钮即可自动播放
  • 该视频广告可重放

测试完成后,将测试网址替换为网页数据视图的网址 预期加载的内容