如何使用MOPUB在Android中通过手动集成实现原生广告

时间:2018-06-05 11:41:54

标签: android ads mopub

尝试将Mopub原生广告整合到Android应用中。

我已经成功整合了来自mopub的横幅广告和插页式广告,但却对原生广告感到苦苦挣扎。

原生广告已加载到onNativeLoad(),但需要有关将原生广告视图附加到主要活动视图的帮助

公共类MainActivity扩展了Activity {

private MoPubView moPubView;
//private MoPubInterstitial mInterstitial;
private MoPubNative moPubNative;
private MoPubNativeNetworkListener moPubNativeNetworkListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    moPubNativeNetworkListener = new MoPubNativeNetworkListener() {
        @Override
        public void onNativeLoad(NativeAd nativeAd) {

            Log.d("MoPub", "Native ad has loaded.");



        }

        @Override
        public void onNativeFail(NativeErrorCode errorCode) {
            Log.d("MoPub", "Native ad failed to load with error: " + errorCode.toString());
        }
    };

    moPubNative = new MoPubNative(this, "11a17b188668469fb0412708c3d16813 ", moPubNativeNetworkListener);

    ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_list_item)
            .mainImageId(R.id.native_main_image)
            .iconImageId(R.id.native_icon_image)
            .titleId(R.id.native_title)
            .textId(R.id.native_text)
            .privacyInformationIconImageId(R.id.native_privacy_information_icon_image)
            .build();


    MoPubStaticNativeAdRenderer moPubStaticNativeAdRenderer = new MoPubStaticNativeAdRenderer(viewBinder);
    moPubNative.registerAdRenderer(moPubStaticNativeAdRenderer);


    EnumSet<RequestParameters.NativeAdAsset> desiredAssets = EnumSet.of(
            RequestParameters.NativeAdAsset.TITLE,
            RequestParameters.NativeAdAsset.TEXT,
            RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT,
            RequestParameters.NativeAdAsset.MAIN_IMAGE,
            RequestParameters.NativeAdAsset.ICON_IMAGE,
            RequestParameters.NativeAdAsset.STAR_RATING
    );

    RequestParameters mRequestParameters = new RequestParameters.Builder()
            .desiredAssets(desiredAssets)
            .build();

    moPubNative.makeRequest();
}

}

2 个答案:

答案 0 :(得分:0)

onNativeLoad()使用NativeAd实例触发时,您需要从MoPub获取预呈现的广告视图,然后将其添加到视图层次结构中的父视图中:

final View adView = adapterHelper.getAdView(null, nativeAdView, nativeAd, new ViewBinder.Builder(0).build());

adView.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
parentView.addView(adView);
  • nativeAdView:您的Android布局,用于存放广告视图。
  • nativeAd:您的原生广告实例由onNativeLoad()回调提供。
  • parentView:您的Android布局包含nativeAdView

答案 1 :(得分:0)

ad_holder中创建了actvity_main.xml

nativead视图会传递给ad_holder

这是一种正确的方法,如果不在下面发表评论。

RelativeLayout adParent = findViewById(R.id.ad_holder);
            View adView = nativeAd.createAdView(getBaseContext(), adParent);
            nativeAd.prepare(adView);
            nativeAd.renderAdView(adView);
            adParent.addView(adView);