#IronSource Android游戏banner广告快速接入指南

最近IronSource开始支持Banner广告了,总结一下Android游戏接入IronSource Banner广告的一些要点:

官方文档

https://developers.ironsrc.com/ironsource-mobile/android/banner-integration-android

详细接入步骤

1. 初始化Banner广告

  • 1). 创建banner的容器 FrameLayout
  • 2). 创建banner view (设置尺寸)
  • 3). 将banner view添加到容器里
  • 4). 将banner容器添加到MainActivity
  • 5). 设置banner回调方法
  • 6). 加载banner广告
    public static void initBanner(boolean showAfterLoad){
        // _app = MainActivity
        if(_app == null){
            Log.w(TAG, "initBanner: App not inited");
            return;
        }

        // 1). 创建banner容器
        FrameLayout bannerContainer = new FrameLayout(_app);
        FrameLayout.LayoutParams containerParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT
        );
        containerParams.gravity = Gravity.BOTTOM;
        _bannerContainer = bannerContainer;

        // 2). 创建banner view
        IronSourceBannerLayout banner = IronSource.createBanner(_app, ISBannerSize.BANNER);
        _banner = banner;
        FrameLayout.LayoutParams bannerParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);

        // 3). 将banner view添加到容器里
        bannerContainer.addView(banner, 0, bannerParams);

        // 4). 把bannerContainer添加到MainActivity里
        _app.addContentView(bannerContainer, containerParams);

        // 5). 设置banner回调方法
        banner.setBannerListener(new BannerListener() {
            @Override
            public void onBannerAdLoaded() {
                // Called after a banner ad has been successfully loaded
                Log.d(TAG, "onBannerAdLoaded: ===> Banner loaded");
            }

            @Override
            public void onBannerAdLoadFailed(IronSourceError error) {
                Log.d(TAG, "onBannerAdLoadFailed: ===> Banner load failed: " + error.getErrorMessage());
                // Called after a banner has attempted to load an ad but failed.
                _app.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        _bannerContainer.removeAllViews();
                        // TODO: 也许可以尝试重新创建banner view并load
                    }
                });
            }

            @Override
            public void onBannerAdClicked() {
                // Called after a banner has been clicked.
                Log.d(TAG, "onBannerAdClicked: ===> Banner Clicked");
            }

            @Override
            public void onBannerAdScreenPresented() {
                // Called when a banner is about to present a full screen content.
                Log.d(TAG, "onBannerAdScreenPresented: ===> Banner Showed");
            }

            @Override
            public void onBannerAdScreenDismissed() {
                // Called after a full screen content has been dismissed
                Log.d(TAG, "onBannerAdScreenDismissed: ===> Banner Hided");
            }

            @Override
            public void onBannerAdLeftApplication() {
                // Called when a user would be taken out of the application context.
                Log.d(TAG, "onBannerAdLeftApplication: ");
            }
        });

        // 6). 加载banner广告
        IronSource.loadBanner(_banner);
        
        // 7). 如果不需要马上显示,可以隐藏
        if(!showAfterLoad){
            hideBanner();
        }
    }

2. 加载Banner

IronSource.loadBanner(_banner);

3. 销毁Banner

IronSource.destroyBanner(_banner);

如果销毁了的话,应该是要重新创建banner view的。

4. 显示Banner

    public static void showBanner(){
        if(_banner == null){
            Log.w(TAG, "showBanner: Banner Not Inited");
            return;
        }
        Log.d(TAG, "showBanner: ===> start loading banner");
        _banner.setVisibility(View.VISIBLE);
    }

5. 隐藏Banner

    public static void hideBanner(){
        if(_banner == null){
            Log.w(TAG, "hideBanner: Banner Not Inited");
            return;
        }

        Log.d(TAG, "hideBanner: ===> hide banner");
        _banner.setVisibility(View.INVISIBLE);
    }