Native广告集成说明
原生(Native)
1. 确定AdHubSDK的主SDK文件已经拷贝到工程文件的libs子目录下参考
2. 添加广告代码,添加广告代码前请确定已初始化SDK
- 例如:
NativeAd nativeAd = new NativeAd(activity, mAdUnitId, new NativeAdListener() {
@Override
public void onAdFailed(int errorcode) {
Toast.makeText(activity, "onAdFailedToLoad reason: " + errorcode, Toast.LENGTH_LONG).show();
}
@Override
public void onAdLoaded(NativeAdResponse response) {
// 一个广告只允许展现一次,多次展现、点击只会计入一次
//返回设置的广告的多个图片的URL,SDK并未处理加载urls里面的图片,需要集成者自己去加载展示
//目前SDK只会返回一张广告图片,数组取第一个即可
ArrayList<String> imageUrls = response.getImageUrls();
//返回设置的广告的多个视频流的URL,SDK并未处理加载urls里面的视频,需要集成者自己去加载展示
ArrayList<String> vedioUrls = response.getVedioUrls();
//返回设置的广告的多个文本信息
ArrayList<String> texts = response.getTexts();
//根据广告法新规定,必须给广告加入广告标识和广告来源。SDK通过以下方法返回广告字样及广告来源标识图片。
//需要开发者分别放置于广告左下和右下角
//广告字样
ServerResponse.AdLogoInfo adUrl = response.getAdUrl();
//广告来源标识
ServerResponse.AdLogoInfo adLogoInfo = response.getlogoUrl();
// ServerResponse.AdLogoInfo结构
public static class AdLogoInfo {
public static int TYPE_PIC = 0;
public static int TYPE_TEXT = 1;
String adurl;
int type = 0;
}
//其中属性type为广告表示的类型共2种:图片和文字,如果type==TYPE_PIC则属性adurl是图片的url
//如果是type==TYPE_TEXT则属性adurl是文字字符串
//开发者自己实现加入广告字样亦可.
//SDK提供此方法用来将response返回的广告图片加入logo。开发者可使用此方法直接获取广告图片
//目前SDK仅会返回一张广告素材
NativeAdUtil.getOneAdBitmap(response, new ImageManager.BitmapLoadedListener() {
@Override
public void onBitmapLoaded(Bitmap bitmap) {
((ImageView) findViewById(R.id.iv_native2)).setImageBitmap(bitmap);
Log.e("onBitmapLoaded","onBitmapLoaded");
}
}
});
//此方法用于注册素材显示的view,集成者必须调用此方法注册此次广告,这样才能统计广告素材的展示。
NativeAdUtil.registerTracking(nativeResponse, findViewById(R.id.appinstall_layout), new NativeAdEventListener() {
@Override
//显示素材的view被点击时回调
public void onAdWasClicked() {
Toast.makeText(NativeActivity.this, "onAdWasClicked", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdWillLeaveApplication() {
Toast.makeText(NativeActivity.this, "onAdWillLeaveApplication", Toast.LENGTH_SHORT).show();
}
});
//加载广告
nativeAd.loadAd();
- 构造NativeAd:
// activity Context
// mAdUnitId 广告位ID
// nativeAdListener 事件监听器
NativeAd nativeAd = new NativeAd(activity, mAdUnitId, nativeAdListener);
控制接口: nativeAd.loadAd(); // 加载广告
NativeAdListener接口定义:
/**
* Define the events relating the status of a NativeAdRequestImpl
*/
public interface NativeAdListener {
/**
* Called when a native ad has successfully loaded.
* If the developer requested shouldLoadIcon true or shouldLoadImage true.
* This method will be called only after the image resources have been retrieved.
* If errors are encountered in resource retrieval this method will still be called.
* Errors encountered in resource retrieval will result in the getImage() or getIcon()
* returning null respectively.
* @param response a NativeAdResponse
*/
public void onAdLoaded(NativeAdResponse response);
/**
* Called when a nativead ad call has logFailed
* @param errorcode reason the call logFailed. Error codes TBD
*/
public void onAdFailed(int errorcode);
}
- NativeAdResponse结构定义:
public interface NativeAdResponse {
public enum Network {
ADHUB,
BAIDU,
AFP,
GDT,
}
public enum Type {
CONTENT,
APP_INSTALL
}
/*
* @return Ad type
*/
public Type getNativeAdType();
/**
*
* @return Network Identifier object
*/
public Network getNetworkIdentifier();
/**
* The Headline for this nativead ad
*
* @return empty string "" if not present
*/
public String getHeadline();
/**
* The URL of the main image can be used to manage image resources manually
*
* @return The URL of the main image or empty string "" if not present
*/
public String getImageUrl();
/**
* Retrieve the main image resource, null if there was no image available or if
* shouldLoadImage() was set to false.
*
* @return The Bitmap of the main image
*/
public Bitmap getImage();
/**
* Set the main image resource
*
* @param bitmap The Bitmap of the main image
*/
public void setImage(Bitmap bitmap);
/**
* The text body of the ad , the text body
*
* @return empty string "" if not present
*/
public String getBody();
/**
* The URL of the Icon image resource can be used to manage image resources manually
*
* @return The URL of the Icon or empty string "" if not present
*/
public String getIconUrl();
/**
* Retrieve the icon image resource, null if there was no image available or if
* shouldLoadIcon returned false for the ad call
*
* @return The Bitmap of the Icon
*/
public Bitmap getIcon();
/**
* Set the icon image resource
*
* @param bitmap The Bitmap of the main icon
*/
public void setIcon(Bitmap bitmap);
/**
* The text for the call to action
*
* @return empty string "" if not present
*/
public String getCallToAction();
/**
* Retrieve rating from the nativead ad response.
*/
public double getStarRating();
/**
* Return the store information.
*/
public String getStore();
/**
* Return the price info for app install.
*/
public String getPrice();
/**
* Return the advertiser info for content.
*/
public String getAdvertiser();
/**
* Retrieve a map of all elements within the nativead ad response.
*
* @return A map of the nativead ad elements
*/
public HashMap<String, Object> getNativeElements();
/**
* Determine if this Response has expired
*
* @return true if it has expired
*/
public boolean hasExpired();
/**
* Register view for tracking
*
* @param view the container of nativead ad
* @param listener the mAdDispatcher of the ad events, can be null
* @return True if successful
*/
public boolean registerView(View view, NativeAdEventListener listener);
/**
* Register a list of clickable views for tracking
*
* @param view the the container of nativead ad
* @param clickables the list of clickables
* @param listener the mAdDispatcher of the ad events, can be null
* @return True if successful
*/
public boolean registerViewList(View view, List<View> clickables, NativeAdEventListener listener);
/**
* Unregister views for this response
* This will be mDestroyed once views are unregistered
*/
public void unregisterViews();
/**
* Destroy the response
*/
public void destroy();
/**
* Retrieve the main image resources
*
* @return The Bitmap of the main imageUrls
*/
public ArrayList<String> getImageUrls();
/**
* Retrieve the main vedio resource urls
*
* @return The urls of the main vedio
*/
public ArrayList<String> getVedioUrls();
/**
* Retrieve the main text resource
*
* @return The text infos
*/
public ArrayList<String> getTexts();
}
PS:更多设置请参考Demo中的com.hubcloud.adhubsdkdemo.NativeActivity的代码.