Tích hợp WebView API for Ads (API WebView cho quảng cáo)
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
API WebView cho quảng cáo cung cấp các tín hiệu ứng dụng cho thẻ trong WebView
, giúp cải thiện khả năng kiếm tiền cho những nhà xuất bản đã cung cấp nội dung và bảo vệ nhà quảng cáo khỏi nội dung không thích hợp.

Cách hoạt động
Việc giao tiếp với SDK quảng cáo trên thiết bị di động của Google chỉ xảy ra khi phản hồi các sự kiện quảng cáo do bất kỳ sự kiện nào sau đây kích hoạt:
SDK sẽ thêm trình xử lý thông báo vào WebView
đã đăng ký để theo dõi các sự kiện quảng cáo này. Để hiểu rõ hơn về cách hoạt động của chức năng này, hãy xem mã nguồn của trang thử nghiệm.
Điều kiện tiên quyết
- SDK Quảng cáo của Google trên thiết bị di động
phiên bản 20.6.0 trở lên.
API Android cấp 21 trở lên.
Thêm thẻ <meta-data>
sau vào tệp AndroidManifest.xml
để bỏ qua việc kiểm tra APPLICATION_ID
. Nếu bạn bỏ lỡ bước này và không cung cấp thẻ <meta-data>
, thì SDK Quảng cáo của Google trên thiết bị di động sẽ gửi một IllegalStateException
khi khởi động ứng dụng.
<!-- Bypass APPLICATION_ID check for web view APIs for ads -->
<meta-data
android:name="com.google.android.gms.ads.INTEGRATION_MANAGER"
android:value="webview"/>
Đăng ký khung hiển thị web
Gọi registerWebView()
trên luồng chính để thiết lập kết nối với các trình xử lý JavaScript trong mã AdSense hoặc Thẻ nhà xuất bản của Google trong mỗi phiên bản WebView
. Bạn nên thực hiện việc này càng sớm càng tốt, chẳng hạn như trong phương thức onCreate()
của MainActivity
.
Kotlin
import android.webkit.CookieManager
import android.webkit.WebView
import com.google.android.gms.ads.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.gms.ads.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);
}
}
Kiểm tra quá trình tích hợp
Trước khi sử dụng URL của riêng mình, bạn nên tải URL sau để kiểm tra quá trình tích hợp:
https://google.github.io/webview-ads/test/#api-for-ads-tests
URL kiểm thử sẽ hiển thị các thanh trạng thái màu xanh lục cho một lần tích hợp thành công nếu bạn đáp ứng các điều kiện sau:
WebView
được kết nối với SDK Quảng cáo của Google trên thiết bị di động
Các bước tiếp theo
- Thu thập sự đồng ý trong
WebView
. API Web view cho Quảng cáo không truyền sự đồng ý được thu thập trong bối cảnh ứng dụng di động bằng cách sử dụng TCF phiên bản 2.0 của IAB hoặc khuôn khổ tuân thủ CCPA của IAB cho các thẻ trong thành phần hiển thị web của bạn. Nếu bạn muốn triển khai một quy trình đồng ý duy nhất với tư cách là chủ sở hữu của cả WebView
và nội dung web tương ứng đang được kiếm tiền, hãy hợp tác với nền tảng quản lý sự đồng ý của bạn để thu thập sự đồng ý trong bối cảnh WebView
.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eThe Web View APIs for Ads improves ad monetization by making app signals available to ad tags within your Android WebView.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Mobile Ads SDK communicates with your WebView in response to ad events triggered by AdSense, Google Publisher Tag, or IMA for HTML5.\u003c/p\u003e\n"],["\u003cp\u003eTo use the Web View APIs for Ads, you need Google Mobile Ads SDK 20.6.0 or higher, Android API level 21 or higher, and to register your WebView using \u003ccode\u003eMobileAds.registerWebView()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou need to gather consent within the WebView context as it's not automatically propagated from the mobile app's consent flow.\u003c/p\u003e\n"]]],[],null,["# Integrate the WebView API for Ads\n\nThe web view APIs for ads makes app signals available to the tags in your\n\n[`WebView`](//developer.android.com/reference/android/webkit/WebView), helping to improve monetization for the\npublishers that provided the content and protect advertisers from spam.\n\n\n### How it works\n\nCommunication with the\n\nGoogle Mobile Ads SDK\n\nonly happens in response to ad\nevents triggered by any of the following:\n\n- [AdSense code](//support.google.com/adsense/answer/9274634)\n- [Google Publisher Tag](//support.google.com/admanager/answer/181073)\n- [IMA for HTML5](//support.google.com/adsense/answer/6391192)\n\nThe SDK adds message handlers to the registered `WebView` to listen for\nthese ad events. For a better sense of how this works, view the\n[source code](//github.com/google/webview-ads/blob/main/test/index.html) of the\ntest page.\n\n### Prerequisites\n\n- [Google Mobile Ads SDK](/admob/android/quick-start#import_the_mobile_ads_sdk) version 20.6.0 or higher.\n- Android API level 21 or higher.\n\n- Add the following `\u003cmeta-data\u003e` tag in your `AndroidManifest.xml` file to\n bypass the check for the `APPLICATION_ID`. If you miss this step and don't\n provide the `\u003cmeta-data\u003e` tag, the\n\n Google Mobile Ads SDK\n\n throws an\n [`IllegalStateException`](//developer.android.com/reference/java/lang/IllegalStateException)\n on app start.\n\n \u003c!-- Bypass APPLICATION_ID check for web view APIs for ads --\u003e\n \u003cmeta-data\n android:name=\"com.google.android.gms.ads.INTEGRATION_MANAGER\"\n android:value=\"webview\"/\u003e\n\n\u003cbr /\u003e\n\n### Register the web view\n\nCall\n\n[`registerWebView()`](/admob/android/reference/com/google/android/gms/ads/MobileAds#registerWebView(android.webkit.WebView))\n\non the main thread to establish a connection with the JavaScript handlers in the\nAdSense code or Google Publisher Tag within each `WebView` instance. This\nshould be done as early as possible, such as in the\n\n`onCreate()` method of your `MainActivity`.\n\n\n### Kotlin\n\n import android.webkit.CookieManager\n import android.webkit.WebView\n import com.google.android.gms.ads.MobileAds\n\n class MainActivity : AppCompatActivity() {\n lateinit var webView: WebView\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_main)\n webView = findViewById(R.id.webview)\n\n // Let the web view accept third-party cookies.\n CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)\n // Let the web view use JavaScript.\n webView.settings.javaScriptEnabled = true\n // Let the web view access local storage.\n webView.settings.domStorageEnabled = true\n // Let HTML videos play automatically.\n webView.settings.mediaPlaybackRequiresUserGesture = false\n\n // Register the web view.\n MobileAds.registerWebView(webView)\n }\n }\n\n### Java\n\n import android.webkit.CookieManager;\n import android.webkit.WebView;\n import com.google.android.gms.ads.MobileAds;\n\n public class MainActivity extends AppCompatActivity {\n private WebView webView;\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n webView = findViewById(R.id.webview);\n\n // Let the web view accept third-party cookies.\n CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);\n // Let the web view use JavaScript.\n webView.getSettings().setJavaScriptEnabled(true);\n // Let the web view access local storage.\n webView.getSettings().setDomStorageEnabled(true);\n // Let HTML videos play automatically.\n webView.getSettings().setMediaPlaybackRequiresUserGesture(false);\n\n // Register the web view.\n MobileAds.registerWebView(webView);\n }\n }\n\n### Test your integration\n\nBefore using your own URL, we recommend that you load the following URL to test\nthe integration: \n\n https://google.github.io/webview-ads/test#api-for-ads-tests\n\nThe test URL shows green status bars for a successful integration if the\nfollowing conditions apply:\n\n- `WebView` connected to the Google Mobile Ads SDK\n\n### Next steps\n\n- Gather consent in `WebView`. The Web view APIs for Ads doesn't propagate consent collected in the mobile app context using [IAB TCF v2.0](//iabeurope.eu/tcf-2-0/) or [IAB CCPA](//iabtechlab.com/wp-content/uploads/2019/11/Technical-Specifications-FAQ-US-Privacy-IAB-Tech-Lab.pdf) compliance frameworks to the tags in your web views. If you're interested in implementing a single consent flow as the owner of both the `WebView` and its corresponding web content being monetized, work with your consent management platform to gather consent in the `WebView` context."]]