DU Ad platform简介
DU Ad platform,即百度移动广告平台,该平台面向个人和企业用户,提供广告接入服务,以SDK的方式集成到App中。
集成入门
添加应用和广告单元
步骤:
1、打开官网http://ad.duapps.com/zh_CN/,注册登录
2、选择"我的应用"标签,添加应用
3、点击已创建应用的右侧编辑按钮,进入编辑页面,创建广告单元
创建应用:
创建广告单元:
4、通过前3个步骤最终会自动生成App ID和广告位ID
配置应用ID
<application>
<meta-data
android:name="app_license"
android:value="广告ID" />
</application>
配置广告ID
在assets中添加json.txt文件,文件内容配置如下:
{
"native": [
{
"pid": "159748",
"fbids": [
""
]
}
],
"list": [
{
"pid": "159748",
"fbids": ""
}
]
}
初始化SDK
@Override
public void onCreate() {
...
// init Baidu ad sdk
DuAdNetwork.init(this, getConfigJSON(getApplicationContext()));
}
private String getConfigJSON(Context context) {
BufferedInputStream bis = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
bis = new BufferedInputStream(context.getAssets().open("json.txt"));
byte[] buffer = new byte[4096];
int readLen = -1;
while ((readLen = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readLen);
}
} catch (IOException e) {
Log.e("", "IOException :" + e.getMessage());
} finally {
closeQuietly(bis);
}
return bos.toString();
}
private void closeQuietly(Closeable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (IOException e) {
// empty
}
}
广告加载
加载单条广告
private void initData() {
nativeAd = new DuNativeAd(this, PID, CACHESZIE);
nativeAd.setMobulaAdListener(mListener);
nativeAd.load();
}
DuAdListener mListener = new DuAdListener() {
@Override
public void onError(DuNativeAd ad, AdError error) {
Log.d(TAG, "onError : " + error.getErrorCode());
}
@Override
public void onClick(DuNativeAd ad) {
Log.d(TAG, "onClick : click ad = " + ad);
}
@Override
public void onAdLoaded(final DuNativeAd ad) {
Log.d(TAG, "onAdLoaded, ad = " + ad);
if (ad != null) {
mNativeAd = ad;
}
}
};
加载多条广告
public void init(AppsDataCallback callback) {
adsManager = new DuNativeAdsManager(mContext.getApplicationContext(), PID, CACHESZIE);
adsManager.setListener(new AdListListener(callback));
adsManager.load();
}
private class AdListListener implements AdListArrivalListener {
private AppsDataCallback mCallback;
public AdListListener(AppsDataCallback callback) {
mCallback = callback;
}
@Override
public void onAdLoaded(List arg0) {
Log.d(TAG, "onAdLoaded, list.size = " + (arg0 != null ? arg0.size() : null));
if (arg0 == null || arg0.isEmpty()) {
return;
}
List<NativeAd> lists = new ArrayList<>();
NativeAd nativeAD = null;
for (int i = 0; i < arg0.size(); i++) {
//获取单个广告对象
nativeAD = (NativeAd) arg0.get(i);
if (nativeAD != null) {
lists.add(nativeAD);
}
}
if (lists.isEmpty()) {
return;
}
if (mCallback != null) {
mCallback.onSuccess(adsApps);
}
}
//返回广告错误码
@Override
public void onAdError(AdError arg0) {
Log.d(TAG, "onError : " + arg0.getErrorCode());
if (mCallback != null) {
// mCallback.onError(e);
}
}
}
广告点击
百度原生广告的点击,需要往NativeAd对象注册被点击的View实现,开发者干预不了过程。
NativeAd.registerViewForInteraction(View view);
广告点击事件监听
虽然点击事件干预不了,但是百度提供了点击的回调,我们可以监听到广告是否被成功点击了。
nativeAd.setMobulaAdListener(new DuAdDataCallBack() {
@Override
public void onAdLoaded(NativeAd data) {
}
@Override
public void onAdError(AdError error) {
}
@Override
public void onAdClick() {
}
});
踩坑
- 在国内,需要翻墙网络才能获取广告数据
- 百度返回的原生广告数据,是一组混合类型的广告,没法过滤特定类型的广告
- 应用下载类型的广告,没有包名去过滤已安装的应用