複数の広告リクエストを処理する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
IMA SDK のほとんどの用途では、一度に管理する広告リクエストは 1 つだけです。ただし、ユーザーが動画を選択する前に広告データをプリロードするなど、一部のエッジケースの実装では、複数のリクエストを同時に行う必要がある場合があります。広告リクエストは非同期で行われるため、適切なアド マネージャーが適切なコンテキストに関連付けられていることを確認するのは、困難な作業に思えるかもしれません。
複数のアド マネージャーを区別するプロセスを簡素化するため、Android 向け IMA SDK では、パブリッシャーは任意の値またはオブジェクトを広告リクエストの UserRequestContext フィールドに渡すことができます。この値またはオブジェクトは、AdsManagerLoadedEvent ハンドラで getUserRequestContext() メソッドを使用して取得できます。
例
...
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);
...
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-31 UTC。
[null,null,["最終更新日 2025-08-31 UTC。"],[[["\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 ..."]]