谷歌AdMob广告接入(横幅广告)
第一步:翻墙注册并登陆AdMob开发者平台
第二步:添加广告,获取广告单元ID
第三步:导入sdk,
android studio可直接关联依赖:com.google.android.gms:play-services-ads:8.1.0
第四步:创建布局
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/my_banner_ad_unit_id"/>
第五步:显示广告:
public class MainActivity extends Activity {
AdView mAdView;
AdRequest adRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
Log.e("广告结束","---------------------------");
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
Log.e("请求失败", "---------------------------");
}
@Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
Log.e("用户离开应用,跳入广告", "---------------------------");
}
@Override
public void onAdOpened() {
super.onAdOpened();
Log.e("开始打开广告,屏幕被覆盖", "---------------------------");
}
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.e("广告完成已加载", "---------------------------");
}
});
}
public void showAd2(View v){
requestNewInterstitial();
mAdView.loadAd(adRequest);
Toast.makeText(getApplication(),"横幅广告",Toast.LENGTH_LONG).show();
}
public void requestNewInterstitial() {
adRequest = new AdRequest.Builder()
.addTestDevice("你的测试手机的MD5值")
.build();
}
}
更多可访问AdMob官网:
https:
https:
效果图: