Menu

Banner Ad

1. Implementation of abstract methods for banner ads and callbacks for advertising events

1.1 Abstract methods that require additional implementation for banner ads

Abstract methodParameter descriptionReturn valueFunctionWhether it must be implemented
getBannerView-ViewNeed to return the successfully loaded BannerView objectYes

1.2 Banner ad event callback

Use the CustomBannerEventListener member variable of CustomBannerAdapter to implement callbacks for advertising events

Callback methodParameter descriptionDescription
onBannerAdClicked-Callback executed when the ad is clicked to the developer< /td>
onBannerAdShow-The callback executed when the ad is displayed is sent to the developer (it does not need to be called currently, because the aggregation has its own set of display statistics)
onBannerAdClose- Callback to the developer executed when the ad is closed

Note: When using the member variable CustomBannerEventListener, you need to perform null processing because the Listener object may be recycled inside the aggregation SDK

1.3 Sample Code

public class FacebookBannerAdapter extends CustomBannerAdapter {

    private String unitid = "";
    AdView mBannerView;
    String size = "";

    @Override
    public void loadCustomNetworkAd(final Context activity, Map<String, Object> serverExtras, Map<String, Object> localExtras) {

        if (serverExtras.containsKey("unit_id")) {
            unitid = (String) serverExtras.get("unit_id");

        } else {
            if (mLoadListener != null) {
                mLoadListener.onAdLoadError("", "facebook unitid is empty.");
            }
            return;
        }

        FacebookInitManager.getInstance().initSDK(activity.getApplicationContext(), serverExtras);

        if (serverExtras.containsKey("size")) {
            size = serverExtras.get("size").toString();
        }

        final AdListener adListener = new AdListener() {
            @Override
            public void onAdLoaded(Ad ad) {
                mBannerView = (AdView) ad;
                if (mLoadListener != null) {
                    mLoadListener.onAdCacheLoaded();

                }
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                if (mLoadListener != null) {
                    mLoadListener.onAdLoadError(adError.getErrorCode() + "", adError.getErrorMessage());
                }
            }

            @Override
            public void onAdClicked(Ad ad) {
                if (mImpressionEventListener != null) {
                    mImpressionEventListener.onBannerAdClicked();
                }
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                if (mImpressionEventListener != null) {
                    mImpressionEventListener.onBannerAdShow();
                }
            }
        };

        AdView adView = null;
        switch (size) {
            case "320x50":
                adView = new AdView(activity, unitid, AdSize.BANNER_HEIGHT_50);
                break;
            case "320x90":
                adView = new AdView(activity, unitid, AdSize.BANNER_HEIGHT_90);
                break;
            case "300x250":
                adView = new AdView(activity, unitid, AdSize.RECTANGLE_HEIGHT_250);
                break;
            default:
                adView = new AdView(activity, unitid, AdSize.BANNER_HEIGHT_50);
                break;
        }
        adView.loadAd(adView.buildLoadAdConfig().withAdListener(adListener).build());
    }

    @Override
    public View getBannerView() {
        return mBannerView;
    }

    @Override
    public void destory() {
        if (mBannerView != null) {
            mBannerView.destroy();
            mBannerView = null;
        }
    }

    @Override
    public String getNetworkSDKVersion() {
        return FacebookInitManager.getInstance().getNetworkVersion();
    }

    @Override
    public String getNetworkName() {
        return FacebookInitManager.getInstance().getNetworkName();
    }

    @Override
    public boolean setUserDataConsent(Context context, boolean isConsent, boolean isEUTraffic) {
        return false;
    }

    @Override
    public String getNetworkPlacementId() {
        return unitid;
    }
}


Last modified: 2025-05-30Powered by