Skip to content

Native Integration

liuluchao edited this page May 16, 2016 · 16 revisions

NativeAd Integration for Android

获取Native广告样式步骤:

 1:通过native广告数据的接口获取INative广告数据对象;
	1.1:base包是通过NativeAdManagerMini接口,ext包是通过NativeAdManagerEx接口
 2:根据自己的需求布局要展示的native广告样式;
 3:将获取的native广告数据展示在自己的布局之上;
 4:注册广告;

1: 获取native广告数据

 1.1 base包:

 if(nativeAdManagerMini == null){
    nativeAdManagerMini = new NativeAdManagerMini(this, "Your posid");
 }
 nativeAdManagerMini.setNativeAdListener(new INativeAdLoaderListener() {
	       @Override
	       public void adLoaded() {
	       		 INativeAd ad = nativeAdManagerMini.getAd();
   		  }

        @Override
        public void adFailedToLoad(int i) {
        }


        @Override
        public void adClicked(INativeAd ad) {
        }
    });     
	nativeAdManagerMini.loadAd();

1.2 ext包:

 if(nativeAdManagerEx == null){
    nativeAdManagerEx = new NativeAdManagerEx(this, "Your posid");
 }
 nativeAdManagerEx.setNativeAdListener(new INativeAdLoaderListener() {
	       @Override
	       public void adLoaded() {
	       		 INativeAd ad = nativeAdManagerEx.getAd();
   		  }

        @Override
        public void adFailedToLoad(int i) {
        }


        @Override
        public void adClicked(INativeAd ad) {
        }
    });     
	nativeAdManagerEx.loadAd();

2:布局要展示的native广告样式

<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:orientation="vertical"
	android:background="@drawable/ad_border"
	android:layout_height="wrap_content" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="10dp" >
       		 <ImageView
           		 android:id="@+id/big_iv_icon"
				...
           		/>
            <TextView
                android:id="@+id/big_main_title"
                ...
				/>

            <TextView
                android:id="@+id/big_sub_title"
                ...
				/>
      
        	<Button
            	android:id="@+id/big_btn_install"
              	...
  				/>
    </RelativeLayout>
</LinearLayout>

注:如果接入admob广告则需要针对两种不同的admob广告添加两种不同的布局,该两种布局的跟布局分别是

com.google.android.gms.ads.formats.NativeAppInstallAdView、
com.google.android.gms.ads.formats.NativeContentAdView

1:InstallAdView
	<com.google.android.gms.ads.formats.NativeAppInstallAdView
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="match_parent"
		android:layout_height="wrap_content">
		<LinearLayout
    		xmlns:android="http://schemas.android.com/apk/res/android"
    		android:layout_width="match_parent"
    		android:orientation="vertical"
    		android:background="@drawable/ad_border"
    		android:layout_height="wrap_content" >
				
			<ImageView
           		 android:id="@+id/big_iv_icon"
				...
           		/>
            <TextView
                android:id="@+id/big_main_title"
                ...
				/>

            <TextView
                android:id="@+id/big_sub_title"
                ...
				/>
      
        	<Button
            	android:id="@+id/big_btn_install"
              	...
  				/>
		</LinearLayout>
	</com.google.android.gms.ads.formats.NativeAppInstallAdView>

2:ContentAdView
	<com.google.android.gms.ads.formats.NativeContentAdView
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="match_parent"
		android:layout_height="wrap_content" >
		<LinearLayout
    		xmlns:android="http://schemas.android.com/apk/res/android"
    		android:layout_width="match_parent"
    		android:orientation="vertical"
    		android:background="@drawable/ad_border"
    		android:layout_height="wrap_content" >

			<ImageView
           		 android:id="@+id/big_iv_icon"
				...
           		/>
            <TextView
                android:id="@+id/big_main_title"
                ...
				/>

            <TextView
                android:id="@+id/big_sub_title"
                ...
				/>
      
        	<Button
            	android:id="@+id/big_btn_install"
              	...
  				/>
		</LinearLayout>
	</com.google.android.gms.ads.formats.NativeContentAdView>

**具体实现方式;可参考 juhe_V3.4_ext_world/CMAdSdkDemo/src/main/java/com/cmcm/ads/ui/OrionNativeAdview.java**

3:INative数据复制到对应的布局文件上

		//getAd Data
 		String iconUrl = ad.getAdIconUrl();
        String mainImageUrl = ad.getAdCoverImageUrl();
		
		...

		//find Ui
        TextView titleTextView = (TextView) mNativeAdView.findViewById(R.id.big_main_title);
        TextView subtitleTextView = (TextView) mNativeAdView.findViewById(R.id.big_sub_title);
        Button bigButton = (Button) mNativeAdView.findViewById(R.id.big_btn_install);
        TextView bodyTextView = (TextView) mNativeAdView.findViewById(R.id.text_body);

        //fill ad data
        titleTextView.setText(ad.getAdTitle());
        subtitleTextView.setText(ad.getAdTypeName());
        bigButton.setText(ad.getAdCallToAction());
        bodyTextView.setText(ad.getAdBody());

如果接入了admob广告商的广告:

	//内容类广告    
    nativeContentAdView.setBodyView(nativeContentAdView.findViewById(R.id.iv_main));
    //title  textview
    nativeContentAdView.setHeadlineView(nativeContentAdView.findViewById(R.id.big_main_title));
    //icon imageview
    nativeContentAdView.setLogoView(nativeContentAdView.findViewById(R.id.big_iv_icon));
    //body textview
    nativeContentAdView.setAdvertiserView(nativeContentAdView.findViewById(R.id.text_body));
    // download textview
    nativeContentAdView.setCallToActionView(nativeContentAdView.findViewById(R.id.big_btn_install));

	//下载类广告
    nativeAppInstallAdView.setBodyView(nativeAppInstallAdView.findViewById(R.id.iv_main));
    //title  textview
    nativeAppInstallAdView.setHeadlineView(nativeAppInstallAdView.findViewById(R.id.big_main_title));
    //icon imageview
    nativeAppInstallAdView.setIconView(nativeAppInstallAdView.findViewById(R.id.big_iv_icon));
    //body textview
    nativeAppInstallAdView.setStoreView(nativeAppInstallAdView.findViewById(R.id.text_body));
    // download textview
    nativeAppInstallAdView.setCallToActionView(nativeAppInstallAdView.findViewById(R.id.big_btn_install));
    //title  textview

4: 注册广告:

	if (mNativeAd != null) {
        mNativeAd.unregisterView();
    }
    mNativeAd.registerViewForInteraction(mNativeAdView);
   **注,如果没有添加注册,则该广告的展示以及点击是无法上报的**

Clone this wiki locally