1、首先在登录你的admob控制台添加你的应用,如果你的应用已经上线google商店的话则需要选择你的上线的项目, 接入前建议先看下接入文档 developers.google.com/admob/andro…

2、添加完应用后,点击你的应用进入应用信息界面,在广告单元页面添加广告单元,在添加广告单元中选择大中小这个尺寸需要注意的,一旦选择了尺寸,当前这个广告单元则无法再修改其他尺寸,除非重新添加广告单元,三个尺寸版本的宽高都范围值,广告单元可以在这个范围值内任意修改,但是代码里的宽高值不再你所选的范围值之内的话,广告是无法显示出来的,这个需要注意的。



3、添加完广告单元后,进入广告单元界面,有个广告单元的ID,这个代码里需要设置的,自定义模板里的广告类型默认有应用安装和内容,这个是为了增加广告的填充率的,广告样式可以根据各人需求随意修改。如果你只需要应用安装广告类型的话,只需要把内容的勾选去掉就好,个人经过测试,如果两种广告类型的选择的话,大部分显示的是内容类型的。

4.接下来就是添加下依赖和代码了:compile 'com.google.android.gms:play-services-ads:11.0.4'
/**
* admob
*/
private void initNativeAdmob(final View view) {
xrecyIndex.post(new Runnable() {
@Override
public void run() {
CardView cardView = (CardView) view.findViewById(R.id.ad_card_view);
final NativeExpressAdView adView = new NativeExpressAdView(getActivity());
AdSize adSize = new AdSize(AdSize.FULL_WIDTH, 100);
adView.setAdSize(adSize);
adView.setAdUnitId(getString(R.string.admob_app_id));
cardView.addView(adView);
// Load the first Native Express ad in the items list.
cardView.setVisibility(View.GONE);
adView.setVisibility(View.GONE);
loadNativeExpressAd(adView, cardView);
}
});
}
/**
* Loads the Native Express ads in the items list.
*/
private void loadNativeExpressAd(final NativeExpressAdView adView, final CardView cardView) {
// Set an AdListener on the NativeExpressAdView to wait for the previous Native Express ad
// to finish loading before loading the next ad in the items list.
adView.setAdListener(new com.google.android.gms.ads.AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
// The previous Native Express ad loaded successfully, call this method again to
// load the next ad in the items list.
showLog("ADMOB------sucess-----------------");
cardView.setVisibility(View.VISIBLE);
adView.setVisibility(View.VISIBLE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
// The previous Native Express ad failed to load. Call this method again to load
// the next ad in the items list.
showLog("admob---errorCode----------" + errorCode);
showLog("The previous Native Express ad failed to load. Attempting to"
+ " load the next Native Express ad in the items list.");
loadNativeExpressAd(adView, cardView);
}
});
// Load the Native Express ad.
adView.loadAd(new AdRequest.Builder().build());
}
坑点:
- 谷歌广告是依靠于整套google service的基础下的,所以手机必须要安装google service 2.当两种类型都选择上的话,国内手机一直都只是显示内容类型的,应用安装类型的从来没有出现过(个人经过无数次测试得出的结果),如果能接受内容类型的广告的话这个则影响不大,但是当设置成只显示应用下载类型的广告时就比较DT了,我用国内厂商的手机(华为、小米等)显示不出来,邮件给google开发团队也没有具体定位到什么原因,后来找了一台google的亲儿子来测试,终于可以显示出来了。最终还是google 服务的问题。 3.测试google admob不需要vpn的