3

我已经阅读了有关如何在 Admob 中介中实现自定义事件的所有内容。我添加了完整的打包类名,一切都在 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),

任何输入都会有所帮助。

4

1 回答 1

3

与普通 AdMob 实施相比,自定义事件略有不同。在requestBannerAd您创建广告网络的 adview 并请求广告。一旦收到广告(在此onReceiveAd回调中),您调用:

this.bannerListener.onReceivedAd(this.adView);

您已经在代码中执行此操作。调用它时,您是在告诉 AdMob 中介层“嘿,我成功加载了一个广告,这是我的视图供您展示。” 中介层接收您的 adview 并基本上layout.addView(adView)代表您调用(它将它添加为您在应用程序中定义的主 AdView 的子项)。

所以在你的情况下,这段代码应该可以工作。

于 2012-07-26T21:57:42.940 回答