private void load(CustomEventBannerListener customEventBannerListener, MediationAdRequest mediationAdRequest, int slotId, int adSize, Context context) { bannerListener = customEventBannerListener; if (myTargetView == null) { myTargetView = new MyTargetView(context); myTargetView.init(slotId, adSize, false); if (mediationAdRequest != null) { myTargetView.getCustomParams().setGender(mediationAdRequest.getGender()); Date date = mediationAdRequest.getBirthday(); if (date != null && date.getTime() != -1) { GregorianCalendar calendar = new GregorianCalendar(); GregorianCalendar calendarNow = new GregorianCalendar(); calendar.setTimeInMillis(date.getTime()); int a = calendarNow.get(GregorianCalendar.YEAR) - calendar.get(GregorianCalendar.YEAR); if (a >= 0) { myTargetView.getCustomParams().setAge(a); } } } myTargetView.getCustomParams().setCustomParam("mediation", "1"); myTargetView.setListener(myTargetViewListener); } myTargetView.load(); }
@Override public void requestBannerAd(Context context, CustomEventBannerListener customEventBannerListener, String s, AdSize adSize, MediationAdRequest mediationAdRequest, Bundle bundle) { int slotId; try { JSONObject json = new JSONObject(s); slotId = json.getInt(SLOT_ID_KEY); } catch (Exception e) { Log.i(TAG, "Unable to get slotId from parameter json. Probably Admob mediation misconfiguration."); if (customEventBannerListener != null) { customEventBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR); } return; } if (AdSize.MEDIUM_RECTANGLE.equals(adSize)) { load(customEventBannerListener, mediationAdRequest, slotId, MyTargetView.AdSize.BANNER_300x250, context); } else if (AdSize.LEADERBOARD.equals(adSize)) { load(customEventBannerListener, mediationAdRequest, slotId, MyTargetView.AdSize.BANNER_728x90, context); } else { load(customEventBannerListener, mediationAdRequest, slotId, MyTargetView.AdSize.BANNER_320x50, context); } }
@Override public void requestBannerAd(Context context, CustomEventBannerListener listener, String serverParameter, AdSize size, MediationAdRequest mediationAdRequest, Bundle customEventExtras) { /* * In this method, you should: * * 1. Create your banner view. * 2. Set your ad network's listener. * 3. Make an ad request. * * When setting your ad network's listener, don't forget to send the following callbacks: * * listener.onAdLoaded(this); * listener.onAdFailedToLoad(this, AdRequest.ERROR_CODE_*); * listener.onAdClicked(this); * listener.onAdOpened(this); * listener.onAdLeftApplication(this); * listener.onAdClosed(this); */ mSampleAdView = new SampleAdView(context); // Assumes that the serverParameter is the AdUnit for the Sample Network. mSampleAdView.setAdUnit(serverParameter); // Internally, smart banners use constants to represent their ad size, which means a call to // AdSize.getHeight could return a negative value. You can accommodate this by using // AdSize.getHeightInPixels and AdSize.getWidthInPixels instead, and then adjusting to match // the device's display metrics. int widthInPixels = size.getWidthInPixels(context); int heightInPixels = size.getHeightInPixels(context); DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics(); int widthInDp = Math.round(widthInPixels / displayMetrics.density); int heightInDp = Math.round(heightInPixels / displayMetrics.density); mSampleAdView.setSize(new SampleAdSize(widthInDp, heightInDp)); // Implement a SampleAdListener and forward callbacks to mediation. The callback forwarding // is handled by SampleBannerEventFowarder. mSampleAdView.setAdListener(new SampleCustomBannerEventForwarder(listener, mSampleAdView)); // Make an ad request. mSampleAdView.fetchAd(createSampleRequest(mediationAdRequest)); }
/** * Creates a new {@code SampleBannerEventForwarder}. * @param listener An AdMob Mediation {@link CustomEventBannerListener} that should receive * forwarded events. * @param adView A {@link SampleAdView}. */ public SampleCustomBannerEventForwarder( CustomEventBannerListener listener, SampleAdView adView) { this.mBannerListener = listener; this.mAdView = adView; }