Admob Mediation中的自定义事件会添加不受支持的广告网络

时间:2012-07-26 10:22:37

标签: android admob

我已阅读有关如何在Admob Mediation中实施自定义事件的所有内容。 我添加了完整的打包类名,所有内容都在Admob门户中设置。

这是完成的类实现

  public class CustomEvent implements CustomEventBanner, AdListener{

private CustomEventBannerListener bannerListener;
private AdView adView;

@Override
public void requestBannerAd(final CustomEventBannerListener listener,
        final Activity activity,
        String label,
        String serverParameter,
        AdSize adSize,
        MediationAdRequest mediationAdRequest) {
    // Keep the custom event listener for use later.
    this.bannerListener = listener;

    // Determine the best ad format to use given the adSize. If the adSize
    // isn't appropriate for any format, an ad will not fill.
    AdSize bestAdSize = adSize = adSize.findBestSize(
            AdSize.BANNER,
            AdSize.IAB_BANNER,
            AdSize.IAB_LEADERBOARD,
            AdSize.IAB_MRECT,
            AdSize.IAB_WIDE_SKYSCRAPER);
    if (bestAdSize == null) {
        listener.onFailedToReceiveAd();
        return;
    }

    // Initialize an AdView with the bestAdSize and the publisher ID.
    // The publisher ID is the server parameter that you gave when creating
    // the custom event.
    this.adView = new AdView(activity, bestAdSize, serverParameter);

    // Set the listener to register for events.
    this.adView.setAdListener(this);
    // Generate an ad request using custom targeting values provided in the
    // MediationAdRequest.
    AdRequest adRequest = new AdRequest()
    .setBirthday(mediationAdRequest.getBirthday())
    .setGender(mediationAdRequest.getGender())
    .setKeywords(mediationAdRequest.getKeywords())
    .setLocation(mediationAdRequest.getLocation());
    if (mediationAdRequest.isTesting()) {
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
    }

    // Load the ad with the ad request.
    this.adView.loadAd(adRequest);
}

@Override
public void onReceiveAd(Ad ad) {
    this.bannerListener.onReceivedAd(this.adView);
}

@Override
public void onFailedToReceiveAd(Ad ad, ErrorCode errorCode) {
    this.bannerListener.onFailedToReceiveAd();
}

@Override
public void onPresentScreen(Ad ad) {
    this.bannerListener.onClick();
    this.bannerListener.onPresentScreen();
}

@Override
public void onDismissScreen(Ad ad) {
    this.bannerListener.onDismissScreen();
}

@Override
public void onLeaveApplication(Ad ad) {
    this.bannerListener.onLeaveApplication();
}


}

问题是我真的不知道如何在onReceivedAd()中添加使用我的layout.add(adview),

任何输入都会有所帮助。

1 个答案:

答案 0 :(得分:3)

与普通的AdMob实施相比,自定义事件略有不同。在requestBannerAd中,您可以制作广告网络的广告,并请求广告。收到广告后(在onReceiveAd回调中),您可以调用:

this.bannerListener.onReceivedAd(this.adView);

您已在代码中执行此操作。在调用此项时,您会告诉AdMob Mediation图层“嘿,我已成功加载广告,以下是我要显示的视图”。中介层会接收您的广告视图,并基本上代表您致电layout.addView(adView)(它会将其添加为您在应用中定义的主AdView的子广告)。

因此,在您的情况下,此代码应该可以正常工作。