MoPub

Mopub後台設定

參數設定方法

{“appid”:“與艾普特申請的appid”,“appkey”:“與艾普特申請的appkey”} 注意:使用 Mopub SDK 仍須引用 google play services。

CUSTOM EVENT CLASS DATA : {“appid”:“填入向 ADBERT 申請的 appid”,“appkey”:“填入向 ADBERT 申請的 appkey”}

ADBERT 橫幅廣告 Adapter

AdbertMopubBanner.java

public class AdbertMopubBanner extends CustomEventBanner {
    private AdbertADView adbertADView;

    @Override
    protected void loadBanner(final Context context,
                              final CustomEventBannerListener customEventBannerListener,
                              final Map<String, Object> localExtras,
                              final Map<String, String> serverExtras) {

        adbertADView = new AdbertADView(context);
        String appID = serverExtras.get("appid");
        String appKey = serverExtras.get("appkey");
        adbertADView.setAdUnit(appID, appKey);
        adbertADView.setAdbertAdSize(AdbertAdSize.BANNER);
        adbertADView.setAdListener(new AdbertADListener() {
            @Override
            public void onAdLoaded(String message) {
                customEventBannerListener.onBannerLoaded(adbertADView);
            }

            @Override
            public void onAdFailLoaded(String message) {
                customEventBannerListener.onBannerFailed(NETWORK_NO_FILL);
            }
        });
        adbertADView.loadAd();
    }

    @Override
    protected void onInvalidate() {
        Views.removeFromParent(adbertADView);
        if (adbertADView != null) {
            adbertADView.destroy();
        }
    }
}
ADBERT 插屏廣告 Adapter

AdbertMopubInterstitial.java

public class AdbertMopubInterstitial extends CustomEventInterstitial {

    AdbertInterstitialAD adbertInterstitialAD;

    @Override
    protected void loadInterstitial(final Context context,
                                    final CustomEventInterstitialListener customEventInterstitialListener,
                                    final Map<String, Object> localExtras,
                                    final Map<String, String> serverExtras) {

        adbertInterstitialAD = new AdbertInterstitialAD(context);
        String appID = serverExtras.get("appid");
        String appKey = serverExtras.get("appkey");
        adbertInterstitialAD.setAdUnit(appID, appKey);
        adbertInterstitialAD.setAdListener(new AdbertADListener() {
            @Override
            public void onAdLoaded(String msg) {
                customEventInterstitialListener.onInterstitialLoaded();
            }

            @Override
            public void onAdFailLoaded(String msg) {
                customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.NETWORK_NO_FILL);
            }

            @Override
            public void onAdClosed() {
                customEventInterstitialListener.onInterstitialDismissed();
            }
        });
        adbertInterstitialAD.loadAd();

    }

    @Override
    protected void showInterstitial() {
        if (adbertInterstitialAD != null)
            adbertInterstitialAD.show();
    }

    @Override
    protected void onInvalidate() {
        if (adbertInterstitialAD != null)
            adbertInterstitialAD.destroy();
    }
}
ADBERT 原生性廣告 Adapter

AdbertMopubNative.java

class AdbertMopubNative extends CustomEventNative {
    @Override
    protected void loadNativeAd(@NonNull final Context context,
                                @NonNull final CustomEventNativeListener customEventNativeListener,
                                @NonNull Map<String, Object> localExtras,
                                @NonNull Map<String, String> serverExtras) {
        /**
         * these key / value set are which set into field
         * "custom event class data" at mopub network settings
         *
         * {"appid":"YOUR_APPID","appkey":"YOUR_APPKEY"}
         *
         */
        String appID = serverExtras.get("appid");
        String appKey = serverExtras.get("appkey");

        final AdbertNativeAD adbertNativeAd = new AdbertNativeAD(context);
        final ImpressionTracker impressionTracker = new ImpressionTracker(context);

        adbertNativeAd.setAdUnit(appID, appKey);
        adbertNativeAd.setAdListener(new AdbertADListener() {

            @Override
            public void onAdLoaded(String message) {
                customEventNativeListener.onNativeAdLoaded(new AdbertStaticNativeAd(adbertNativeAd, impressionTracker));
            }

            @Override
            public void onAdFailLoaded(String message) {
                customEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_NO_FILL);
            }
        });

        adbertNativeAd.loadAd();
    }

    static class AdbertStaticNativeAd extends com.mopub.nativeads.StaticNativeAd {
        private View view;
        private AdbertNativeAD nativeAD = null;
        private ImpressionTracker impressionTracker;

        public AdbertStaticNativeAd(@NonNull AdbertNativeAD nativeAD, final ImpressionTracker impressionTracker) {
            if (nativeAD.isReady()) {
                this.nativeAD = nativeAD;
                this.impressionTracker = impressionTracker;

                JSONObject content = nativeAD.getData();

                try {
                    setTitle(content.getString("headline"));
                    setText(content.getString("desc"));
                    setMainImageUrl(content.getString("image"));
                    setIconImageUrl(content.getString("icon"));
                    addExtra("company", content.getString("companyName"));
                    setCallToAction("馬上行動");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }

        @Override
        public void prepare(@NonNull View view) {
            super.prepare(view);
            nativeAD.registerView(view);
            this.view = view;
        }

        @Override
        public void destroy() {
            super.destroy();
            nativeAD.unregisterView(view);
        }
    }
}