Incorporating the SDK

The decompressed SDK consists of a JAR, a javadoc folder and a README.

1. Right click on your app project in Eclipse and choose Properties.

2. Select Java Build Path and the Libraries tab. Then click Add External JARs... to add the Google AdMob Ads JAR.

You should now be able to rebuild your project without any errors.

Adding a com.google.ads.AdView

Android apps are composed of View objects, Java instances the user sees as text areas, buttons and other controls. AdView is simply another View subclass displaying small HTML5 ads that respond to user touch.

The five lines of code it takes to add a banner:

  • Import com.google.ads.*
  • Declare an AdView instance
  • Create it, specifying a unit ID—your AdMob publisher ID
  • Add the view to the UI
  • Load it with an ad

The easiest place to do all this is in your app’s Activity.

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_UNIT_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());
}
}

All that’s left is to properly configure your AndroidManifest.xml.

com.google.ads.AdActivity

In order for your app to properly maintain the Android activity stack while displaying rich advertising it must declare a com.google.ads.AdActivity in its manifest:

<?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>

Permissions

Making ad requests requires the networking permissions INTERNET and ACCESS_NETWORK_STATE, so these must also be declared in the manifest:

<?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>

You can download an example project containing this code here.

The Result

When you now run your app you should see a banner at the top of the screen:

转自:http://blog.sina.com.cn/s/blog_4d142b550100rw4b.html

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 深入理解Android消息队列原理篇 Looper、
  2. Android之我的第一个程序
  3. android 中的Uri
  4. ACOD:如何在eclipse中使用离线文档中的中
  5. MS(1):Android之架构篇
  6. 安卓之 (解决方法)指定安装应用迁移到SD卡
  7. Android Studio 安装完成不能打开等问题
  8. Android Studio查看源码时出现Sources fo
  9. Android列表组件ListView子项的点击事件
  10. 实现Android下的FPS实时显示工具