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