處理多個廣告請求
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
大多數 IMA SDK 用途只需要一次管理單一廣告請求。不過,某些極端情況的實作方式 (例如在使用者選取影片前預先載入廣告資料) 可能需要同時提出多項要求。由於廣告請求是異步傳送,因此確保適當的廣告管理工具與正確的上下文建立關聯,可能會是一項艱鉅的任務。
為簡化區分多個廣告管理員的程序,Android 版 IMA SDK 允許發布商將任何值或物件傳遞至任何廣告請求的 UserRequestContext 欄位。接著,您可以使用 getUserRequestContext() 方法,在 AdsManagerLoadedEvent 處理常式中擷取這個值或物件。
範例
...
adsLoader = sdkFactory.createAdsLoader(context, imaSdkSettings, adDisplayContainer);
Map<String, String> userContextA = new HashMap<String, String>();
Map<String, String> userContextB = new HashMap<String, String>();
userContextA.put("id", "Request A");
userContextB.put("id", "Request B");
userContextA.put("element", "videoElementA");
userContextB.put("element", "videoElementB");
adRequestA.setUserRequestContext(userContextA);
adRequestB.setUserRequestContext(userContextB);
adsLoader.addAdsLoadedListener(
new AdsLoader.AdsLoadedListener() {
@Override
public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {
Map<String, String> context = adsManagerLoadedEvent.getUserRequestContext();
adsManager = adsManagerLoadedEvent.getAdsManager();
Log.i("ImaExample", "Successfully loaded ID: " + context.get("id"));
}
});
adsLoader.addAdErrorListener(
new AdErrorEvent.AdErrorListener() {
@Override
public void onAdError(AdErrorEvent adErrorEvent) {
Map<String, String> context = adErrorEvent.getUserRequestContext();
Log.i("ImaExample", "Error with AdRequest. ID: " + context.get("id"));
Log.i("ImaExample", "Ad Error: " + adErrorEvent.getError().getMessage());
}
});
adsLoader.requestAds(adRequestA);
adsLoader.requestAds(adRequestB);
...
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThe IMA SDK for Android allows for managing multiple, concurrent ad requests by associating a unique context with each request.\u003c/p\u003e\n"],["\u003cp\u003ePublishers can use the \u003ccode\u003eUserRequestContext\u003c/code\u003e field of an ad request to pass in a custom identifier (e.g., a Map).\u003c/p\u003e\n"],["\u003cp\u003eThis identifier can then be retrieved in the \u003ccode\u003eAdsManagerLoadedEvent\u003c/code\u003e or \u003ccode\u003eAdErrorEvent\u003c/code\u003e handlers using \u003ccode\u003egetUserRequestContext()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThis functionality simplifies the process of differentiating between ad managers tied to specific video content or elements.\u003c/p\u003e\n"]]],[],null,["# Handle multiple ad requests\n\nMost uses of the IMA SDK only require managing a single ad request at a time. However some edge case implementations, such as preloading ad data before the user selects a video, may require making multiple concurrent requests. Since ad requests are made asynchronously, ensuring the proper ad manager is associated with the correct context can seem to be a daunting task.\n\nTo simplify the process of differentiating multiple ad managers, the IMA SDK for Android allows publishers to pass in any value or object to the [UserRequestContext](https://developers.google.com/interactive-media-ads/docs/sdks/android/dai/api/reference/com/google/ads/interactivemedia/v3/api/AdsRequest.html#public-abstract-void-setuserrequestcontext-object-userrequestcontext) field of any ad request. This value or object can then be retrieved in the [AdsManagerLoadedEvent](https://developers.google.com/interactive-media-ads/docs/sdks/android/dai/api/reference/com/google/ads/interactivemedia/v3/api/AdsManagerLoadedEvent) handler, by using the method [getUserRequestContext()](https://developers.google.com/interactive-media-ads/docs/sdks/android/dai/api/reference/com/google/ads/interactivemedia/v3/api/AdsManagerLoadedEvent.html#public-abstract-object-getuserrequestcontext).\n\nExample\n-------\n\n ...\n\n adsLoader = sdkFactory.createAdsLoader(context, imaSdkSettings, adDisplayContainer);\n\n Map\u003cString, String\u003e userContextA = new HashMap\u003cString, String\u003e();\n Map\u003cString, String\u003e userContextB = new HashMap\u003cString, String\u003e();\n userContextA.put(\"id\", \"Request A\");\n userContextB.put(\"id\", \"Request B\");\n userContextA.put(\"element\", \"videoElementA\");\n userContextB.put(\"element\", \"videoElementB\");\n adRequestA.setUserRequestContext(userContextA);\n adRequestB.setUserRequestContext(userContextB);\n\n adsLoader.addAdsLoadedListener(\n new AdsLoader.AdsLoadedListener() {\n @Override\n public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {\n Map\u003cString, String\u003e context = adsManagerLoadedEvent.getUserRequestContext();\n adsManager = adsManagerLoadedEvent.getAdsManager();\n Log.i(\"ImaExample\", \"Successfully loaded ID: \" + context.get(\"id\"));\n }\n });\n\n adsLoader.addAdErrorListener(\n new AdErrorEvent.AdErrorListener() {\n @Override\n public void onAdError(AdErrorEvent adErrorEvent) {\n Map\u003cString, String\u003e context = adErrorEvent.getUserRequestContext();\n Log.i(\"ImaExample\", \"Error with AdRequest. ID: \" + context.get(\"id\"));\n Log.i(\"ImaExample\", \"Ad Error: \" + adErrorEvent.getError().getMessage());\n }\n });\n\n adsLoader.requestAds(adRequestA);\n adsLoader.requestAds(adRequestB);\n\n ..."]]