如何在内容之间集成Mopub Native广告

时间:2016-03-10 05:29:30

标签: android ads mopub

我在listview中集成了Mopub原生广告但效果不错,但我希望在我的内容之间展示原生广告,而不是在列表视图中。

我试过这个

MoPubNative.MoPubNativeNetworkListener moPubNativeListener = new MoPubNative.MoPubNativeNetworkListener() {
        @Override
        public void onNativeLoad(NativeAd nativeAd) {
            // ...
        }

        @Override
        public void onNativeFail(NativeErrorCode errorCode) {
            // ...
        }
    };

    MoPubNative moPubNative = new MoPubNative(SingleActivity.this, "ffb8734de73e4d62b93bae99c06db41f", moPubNativeListener);

    ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_layout)
            .mainImageId(R.id.native_ad_main_image)
            .iconImageId(R.id.native_ad_icon_image)
            .titleId(R.id.native_ad_title)
            .privacyInformationIconImageId(R.id.native_ad_daa_icon_image)
            .textId(R.id.native_ad_text)
            .build();

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

    Location exampleLocation = new Location("example_location");
    exampleLocation.setLatitude(23.1);
    exampleLocation.setLongitude(42.1);
    exampleLocation.setAccuracy(100);

    //Specify which native assets you want to use in your ad.
    EnumSet<RequestParameters.NativeAdAsset> assetsSet = EnumSet.of(RequestParameters.NativeAdAsset.TITLE,
            RequestParameters.NativeAdAsset.TEXT,
            RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT,
            RequestParameters.NativeAdAsset.ICON_IMAGE);

    RequestParameters requestParameters = new RequestParameters.Builder()
            .keywords("gender:m,age:27")
            .location(exampleLocation)
            .desiredAssets(assetsSet)
            .build();

    moPubNative.makeRequest(requestParameters);

但正如您所见,我正在加载布局

  ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_layout)

但是如何在我的详细活动内容之间呈现此布局。

感谢。

2 个答案:

答案 0 :(得分:0)

  

Mopub本地手动广告整合示例

     

首先定义你的视图native_advetisement.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">

    <ImageView
        android:id="@+id/native_ad_main_image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        />

    <ImageView
        android:id="@+id/native_ad_icon_image"
        android:layout_width="20dp"
        android:layout_height="20dp"
        />

    <ImageView
        android:id="@+id/native_ad_daa_icon_image"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_below="@+id/native_ad_main_image"/>

    <TextView
        android:id="@+id/native_ad_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/red"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/native_ad_daa_icon_image"
        android:textSize="12dp"
       android:layout_below="@+id/native_ad_main_image"/>

    <TextView
        android:id="@+id/native_ad_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/black"
        android:layout_toRightOf="@+id/native_ad_daa_icon_image"
        android:textSize="11dp"
        android:layout_marginLeft="20dp"
        android:layout_below="@+id/native_ad_title"
        android:layout_marginBottom="10dp"/>


</RelativeLayout>

</LinearLayout>
  

然后准备你的组件。

公共类AdverNative扩展了LinearLayout {

private static String LOG_TAG = AdverNative.class.getName();
private Context mContext;
Activity activity;

public AdverNative(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
    this.activity = (Activity) context;
    initView();
}


private void initView() {
    final ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_advetisement)
            .titleId(R.id.native_ad_title)
            .textId(R.id.native_ad_text)
            .mainImageId(R.id.native_ad_main_image)
            .iconImageId(R.id.native_ad_icon_image)
            .privacyInformationIconImageId(R.id.native_ad_daa_icon_image)
            .build();
    MoPubNative.MoPubNativeNetworkListener moPubNativeNetworkListener = new MoPubNative.MoPubNativeNetworkListener() {
        @Override
        public void onNativeLoad(NativeAd nativeAd) {


            AdapterHelper ah = new AdapterHelper(mContext, 0, 3);
            View v = ah.getAdView(null, AdverNative.this, nativeAd, viewBinder);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            addView(v, params);
        }

        @Override
        public void onNativeFail(NativeErrorCode nativeErrorCode) {

        }
    };

    MoPubNative moPubNative = new MoPubNative(activity, Constants.CODE_NATIVE, moPubNativeNetworkListener);
    moPubNative.registerAdRenderer(new MoPubStaticNativeAdRenderer(viewBinder));
    EnumSet<RequestParameters.NativeAdAsset> assetsSet = 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);

    Location exampleLocation = new Location("example_location");
    exampleLocation.setLatitude(23.1);
    exampleLocation.setLongitude(42.1);
    exampleLocation.setAccuracy(100);
    final String keywords = "";

    RequestParameters mRequestParameters = new     RequestParameters.Builder()
            .location(exampleLocation)
            .keywords(keywords)
            .desiredAssets(assetsSet)
            .build();

    moPubNative.makeRequest(mRequestParameters);
  }

}
  

并像这样使用

<AdverNative
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</AdverNative>

答案 1 :(得分:0)

这是实现它的方法,至少在最新和最好的MoPub SDK中。

    public void onNativeLoad(NativeAd nativeAd) {

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