添加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.1 右键单击Eclipse的工程并选择属性
1.2 选择Java Build Path->Libraries,选择Add External JARs添加Google Admob SDK的jar文件
[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>
[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>
在代码里直接添加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());
}
}
也可以在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>
本文使用的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分钟间隔将会重复出现。
相关文章推荐
- 添加Google Admob到ANDROID应用中
- 添加Google Admob到ANDROID应用中
- 添加Google Admob到ANDROID应用中
- 添加Google Admob到ANDROID应用中
- 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
- Unity3D Android游戏添加google广告的方法——使用AdMob
- 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
- Android 添加Google官方推出应用架构Architecture Components 的依赖
- 用Eclipse添加android软件的GOOGLE admob广告2016年
- Google Admob广告Android 、简单应用
- 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
- Google Android开发者文档系列-创建有内容分享特性的应用之添加一个简单的共享action
- google admob 广告插件添加[android]
- [Android]彻底去除Google AdMob广告
- Android 为应用添加角标(Badge)
- 为您的Android,iOS等应用添加声波传输功能
- Android应用下植入Google广告
- Google应用在Android上的Push机制以及C2DM框架的底层实现
- android 应用之listview添加radiobutton,获取textView
- selinux下添加android:sharedUserId="android.uid.phone"的应用开放访问"/mnt/asec/"