您的位置:首页 > 编程语言 > Go语言

Google广告AdMob的集成和使用

2016-12-23 15:10 183 查看

Google广告AdMob的集成和使用

作者:popovich


效果如图:



第一步

进入AdMob官网注册账号 (用google邮箱的账号)https://apps.admob.com/

如图



填写自己的邮箱后点击下一步



按照提示注册,绑定好手机号就可以登录进去了。

第二步

进入AdMob官网页面点击MONETIZE选项

如图



点击 MONETIZE NEW APP 红色的按钮,进入下一步:



然后:



ps:图中的第三步,set up firebase analytics(绑定到firebase网站的应用),第一次要绑定一下,若绑定过可以跳过。按提示就可以完成绑定

一步步搞完。就创建好了!

第三步

完成后注意下面三个标记的地方,代码中会用到

如图:



点击图中 第3步 那行的此处进入firebase控制台下载配置文件



保存好这个文件就开始搞代码。

第四步

在Android Studio编辑

1.打开Android Studio工程,在libs中添加这个jar包(android-support-multidex.jar),这个jar包是为了解决dex超过64k的限制问题的。google服务包特别大,添加google服务分分钟超限。android-support-multidex.jar 下载地址

2.将刚下下载的json文件添加工程目录下:app目录下



3.在project项目级别的build.gradle中添加:

repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}


4.在Moduel级别的build.gradle中添加:

apply plugin: 'com.google.gms.google-services'//放到最顶部

compile 'com.google.firebase:firebase-ads:9.0.0'
compile files('libs/android-support-multidex.jar')


5.然后再MyApp中写下面的代码:

public class MyApp extends Application {

@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}


6.然后看布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.popovich.huanxin.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<Button
android:textAllCaps="false"
android:text="ShowAD"
android:layout_centerInParent="true"
android:onClick="showAd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</RelativeLayout>




7.然后看代码:

public class MainActivity extends AppCompatActivity {

private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this,"ca-app-pub-9586062507728601~4027689175");
initView();
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
mAdView.loadAd(adRequest);
}

private void initView() {
mAdView = (AdView) findViewById(R.id.adView);
}

/** Called when leaving the activity */
@Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}

/** Called when returning to the activity */
@Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}

/** Called before the activity is destroyed */
@Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}

public void showAd(View view) {
startActivity(new Intent(this,InterLineActivity.class));
}
}


appid要对应:

如图:



8.运行后就会出现这样的效果:我选择的是Banner的效果即,横幅广告



其他效果的代码就不展示了,这是官方的demo地址:

https://codeload.github.com/googleads/googleads-mobile-android-examples/zip/master

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  广告 admob 谷歌