适用于广告的 WebView API 可让 WebView
中的代码使用应用信号,从而帮助提供内容的发布商提升创收效果,并保护广告客户免受垃圾内容的侵扰。
这些应用信号(例如应用 ID 和应用版本)有助于启用报告和定位应用内浏览器广告资源用例,而这些用例原本仅适用于应用流量。
运作方式
与 Google 移动广告 SDK(Beta 版)的通信仅在响应由以下任一因素触发的广告事件时发生:
SDK 会向已注册的 WebView
添加消息处理程序,以监听这些广告事件。如需更好地了解其运作方式,请查看测试页面的源代码。
前提条件
- Google 移动广告 SDK(Beta 版)0.6.0-alpha01 或更高版本。
将应用 ID 传递给 SDK
如果您已有 Ad Manager 应用 ID,请使用现有的应用 ID 初始化 Google 移动广告 SDK(Beta 版)。
如果您没有 Ad Manager 应用 ID,请在初始化 Google 移动广告 SDK(Beta 版)时传入 InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID
作为应用 ID。
Kotlin
MobileAds.initialize(
this@MainActivity,
// Use this application ID to initialize the Google Mobile Ads SDK (beta) if
// you don't have an Ad Manager application ID.
InitializationConfig.Builder(InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID)
.build(),
) {
// Adapter initialization complete.
}
Java
MobileAds.initialize(
this,
// Use this application ID to initialize the Google Mobile Ads SDK (beta) if
// you don't have an Ad Manager application ID.
new InitializationConfig.Builder(InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID)
.build(),
initializationStatus -> {
// Adapter initialization is complete.
});
注册 WebView
在主线程上调用 registerWebView()
,以在每个 WebView
实例中与 AdSense 代码或 Google 发布商代码中的 JavaScript 处理程序建立连接。应尽早执行此操作,例如在 MainActivity
的 onCreate()
方法中执行。
Kotlin
import android.webkit.CookieManager
import android.webkit.WebView
import com.google.android.libraries.ads.mobile.sdk.MobileAds
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
// Register the web view.
MobileAds.registerWebView(webView)
}
}
Java
import android.webkit.CookieManager;
import android.webkit.WebView;
import com.google.android.libraries.ads.mobile.sdk.MobileAds;
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);
// Register the web view.
MobileAds.registerWebView(webView);
}
}
测试您的集成
在使用自己的网址之前,我们建议您加载以下网址来测试集成:
https://google.github.io/webview-ads/test/#api-for-ads-tests
如果符合以下条件,测试网址会显示绿色状态栏,表示集成成功:
WebView
已连接到 Google 移动广告 SDK(Beta 版)
后续步骤
- 在
WebView
中征求用户意见。适用于广告的 WebView API 不会使用 IAB TCF v2.0 或 IAB CCPA 合规性框架将在移动应用情境中收集的用户意见传播到 WebView 中的代码。如果您是WebView
及其相应创收 Web 内容的所有者,并希望实现单一的意见征求流程,请与您的意见征求管理平台合作,在WebView
情境中征求用户意见。