您的位置:首页 > 移动开发 > Android开发

添加Google Admob到ANDROID应用中

2013-06-23 12:08 337 查看
现在的ANDROID大部分还是靠在应用里面植入广告来盈利。Google Admob是一个不错的平台,但是到网络上一搜,发现按照现有的文章的步骤来添加的话,根本不成功。最后还是到Google Admob的官方文档才得到一个详细的步骤。故本文针对其步骤做了一个简要的翻译。

本文使用的Google Admob SDK为4.1.0,原文网址为:http://code.google.com/intl/zh-CN/mobile/ads/docs/android/fundamentals.html

使用Google Admob SDK包括以下三个步骤:

1, 添加SDK到Eclipse工程里

2, 添加com.google.ads.AdActivity

3, 声明必须的网络权限

4, 添加com.google.ads.AdView

1,添加SDK

解压之后的SDK包含一个jar文件,一个docs文件夹和一个README文档。

1.1 右键单击Eclipse的工程并选择属性



1.2 选择Java Build Path->Libraries,选择Add External JARs添加Google Admob SDK的jar文件



2, 添加com.google.ads.AdActivity

为了使你的应用在显示Admob广告的时候正确的维护Activity的栈,必须在AndroidManifest.xml文件添加com.google.ads.AdActivity

[html] view
plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.company"

android:versionCode="1" android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name"

android:debuggable="true">

<activity android:label="@string/app_name" android:name="BannerExample">

<intent-filter>

<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<activity android:name="com.google.ads.AdActivity"

android:configChanges="keyboard|keyboardHidden|orientation"/>

</application>

</manifest>

3,声明权限

广告需要访问网络,必须添加权限。

[html] view
plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.company"

android:versionCode="1" android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name"

android:debuggable="true">

<activity android:label="@string/app_name" android:name="BannerExample">

<intent-filter>

<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<activity android:name="com.google.ads.AdActivity"

android:configChanges="keyboard|keyboardHidden|orientation"/>

</application>

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>

4,添加AdView

有如下两种方式来添加AdView。


4.1 直接在代码中添加

在代码里直接添加AdView需要如下步骤

1,Import com.google.ads.*

2,声明一个AdView实例

3,创建AdView,指定你的Admob Publisher ID

4,添加AdView到UI

5,装载广告

可参考如下示例:

[java] view
plaincopyprint?

import com.google.ads.*;

public class BannerExample extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// Create the adView

AdView adView = new AdView(this, AdSize.BANNER, MY_AD_PUBLISHER_ID);

// Lookup your LinearLayout assuming it’s been given

// the attribute android:id="@+id/mainLayout"

LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

// Add the adView to it

layout.addView(adView);

// Initiate a generic request to load it with an ad

adView.loadAd(new AdRequest());

}

}


4.2 在XML文件中添加

也可以在layout xml文件中添加AdView。

[html] view
plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<com.google.ads.AdView android:id="@+id/adView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

ads:adUnitId="MY_AD_PUBLISHER_ID"

ads:adSize="BANNER"

ads:loadAdOnCreate="true"/>

</LinearLayout>

测试结果



注意:当Admob第一次接收到你的Publisher ID的广告请求时,可能需要最多2分钟来接受广告。当你的Publisher ID有24小时没有使用时,这初始的2分钟间隔将会重复出现。

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