简体   繁体   English

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

[英]How to implement Native ads through Manual Integration in android using MOPUB

Trying to integrate Mopub native ads into android app. 尝试将Mopub原生广告集成到android应用中。

i have successfully integrated banner and interstitial ads from mopub but struggling with native ads. 我已经成功整合了来自mopub的横幅广告和插页式广告,但仍在与原生广告作斗争。

Native Ad is Loaded into onNativeLoad() , but need help with attaching the native ad view to the Mainactivity view 原生广告已加载到onNativeLoad() ,但需要有关将原生广告视图附加到Mainactivity视图的帮助

public class MainActivity extends Activity { 公共类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();
}

} }

When onNativeLoad() fires with an instance of NativeAd , you need to get the pre-rendered ad view from MoPub, then add it to a parent view in your view hierarchy: 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 : your Android layout to house the ad view. nativeAdView :您的Android布局,用于容纳广告视图。
  • nativeAd : your native ad instance provided from the onNativeLoad() callback. nativeAd :从onNativeLoad()回调提供的原生广告实例。
  • parentView : your Android layout to hold the nativeAdView . parentView :您的Android布局,用于保存nativeAdView

created the ad_holder in the actvity_main.xml . ad_holder中创建了actvity_main.xml

The nativead view is passed to that ad_holder . nativead视图将传递给该ad_holder

is it an right approach to do, if not comment below. 这是正确的做法吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM