จัดการคําขอโฆษณาหลายรายการ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การใช้งาน IMA SDK ส่วนใหญ่ต้องจัดการคําขอโฆษณารายการเดียวในแต่ละครั้ง อย่างไรก็ตาม การใช้งานบางกรณีที่เกิดขึ้นไม่บ่อย เช่น การโหลดข้อมูลโฆษณาล่วงหน้าก่อนที่ผู้ใช้จะเลือกวิดีโอ อาจต้องมีการส่งคำขอหลายรายการพร้อมกัน เนื่องจากคำขอโฆษณาสร้างขึ้นแบบไม่พร้อมกัน การตรวจสอบว่า Ad Manager ที่เหมาะสมเชื่อมโยงกับบริบทที่ถูกต้องจึงอาจดูเหมือนเป็นงานที่ยาก
IMA SDK สําหรับ Android ช่วยให้ผู้เผยแพร่โฆษณาส่งค่าหรือออบเจ็กต์ไปยังช่อง UserRequestContext ของคําขอโฆษณาได้ เพื่อลดความซับซ้อนของกระบวนการแยกความแตกต่างระหว่าง Ad Manager หลายรายการ จากนั้นเรียกข้อมูลค่าหรือออบเจ็กต์นี้ในตัวแฮนเดิล 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 ..."]]