IMA SDK का इस्तेमाल करने के ज़्यादातर मामलों में, एक बार में सिर्फ़ एक विज्ञापन दिखाने का अनुरोध को मैनेज करने की ज़रूरत होती है. हालांकि, कुछ खास मामलों में, एक साथ कई अनुरोध करने पड़ सकते हैं. जैसे, उपयोगकर्ता के वीडियो चुनने से पहले विज्ञापन डेटा को प्रीलोड करना. विज्ञापन अनुरोध एसिंक्रोनस तरीके से किए जाते हैं. इसलिए, यह पक्का करना मुश्किल हो सकता है कि सही विज्ञापन मैनेजर, सही कॉन्टेक्स्ट से जुड़ा हो.
एक से ज़्यादा Ad Manager के बीच अंतर करने की प्रोसेस को आसान बनाने के लिए, 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);
...