android - Admob 中介中的自定义事件添加不受支持的广告网络

标签 android admob

我已阅读有关如何在 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),

任何输入都会有所帮助。

最佳答案

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

this.bannerListener.onReceivedAd(this.adView);

您已经在您的代码中这样做了。调用它时,您是在告诉 AdMob 中介层“嘿,我成功加载了一个广告,这是我的 View 供您显示。”中介层接受您的 adview 并代表您调用 layout.addView(adView)(它将其添加为您在应用中定义的主 AdView 的子级)。

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

关于android - Admob 中介中的自定义事件添加不受支持的广告网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11667285/

相关文章:

Android:如何使按钮导致 ListView 项目详细信息(在不同的 Activity 中)(主/详细流程)

android - 广告合并?

Android AdMob 显示不安全的内容

android - 为什么 Firebase 和 Google Analytics 指标不匹配?

java - 将 ArrayList 从 SecondActivity 传递到 MainActivity Android

javascript - 无法在 Cordova 点击div

ios - Google admob GADBannerView 无法识别的选择器

android - AdMob 与我的应用程序集成

java - Admob 发布 ID 错误

android - Nexus 4手机上没有调用onPreviewFrame…而是在模拟器上