Google AdMob Ads Android Fundamentals

Google AdMob Ads Android Fundamentals

  1. Overview
  2. Requirements
  3. Incorporating the SDK
    1. Adding the SDK JAR
    2. AdActivity
    3. Permissions
  4. Adding a com.google.ads.AdView
  5. The Result
  6. What's Next?

Overview

Google AdMob Ads banners use a small portion of the screen to entice users to "click through" to a richer, full-screen experience such as a website or app store page.

To display banners in your Android app, simply incorporate the SDK into your Eclipse project and add acom.google.ads.AdViewto your UI.

Requirements

The Google AdMob Ads SDK for Android requires Android 1.5 or later. Make sure you have thelatest copy of the Android SDKand that you're compiling against at least Android v3.2 (settargetindefault.propertiestoandroid-13).

Incorporating the SDK

Incorporating Google AdMob Ads into your app is a three step process:

  1. Add the SDK JAR to your Eclipse project.
  2. Declarecom.google.ads.AdActivityinAndroidManifest.xml.
  3. Set up required network permissions in the manifest.

Adding the SDK JAR

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

1. Right click on your app project in Eclipse and chooseProperties.

2. SelectJava Build Pathand theLibrariestab. Then clickAdd External JARs...to add the Google AdMob Ads JAR.

com.google.ads.AdActivity

The AdMob Ads SDK requires thatcom.google.ads.AdActivitybe declared in your app'sAndroidManifest.xml:

<?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|screenLayout|uiMode|screenSize|smallestScreenSize"/>
 </application>
</manifest>

Permissions

Making ad requests requires the networking permissionsINTERNETandACCESS_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|screenLayout|uiMode|screenSize|smallestScreenSize"/>
 </application>
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

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

Adding a com.google.ads.AdView

Android apps are composed ofViewobjects, Java instances the user sees as text areas, buttons and other controls.AdViewis simply anotherViewsubclass displaying small HTML5 ads that respond to user touch.

Like anyView, anAdViewmay be created either purely in code or largely in XML.

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

  • Importcom.google.ads.*
  • Declare anAdViewinstance
  • 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’sActivity.

import com.google.ads.*;

public class BannerExample extends Activity {
 private AdView adView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // Create the 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());
 }

 @Override
 public void onDestroy() {
  adView.destroy();
  super.onDestroy();
 }
}

You can download an example project containing this codehereand may alternatelycreate your banner in XML.

The Result

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

Note:The very first time AdMob sees your publisher ID it may take up to two minutes to receive an ad. This initial two minute lag will recur every time the ID goes unused for 24 hours.

Warning:All new Android apps created after October 14, 2011 will require anAdMob SDKthat was released on or after March 15, 2011. This corresponds to version 4.0.2+ for Android. If you downloaded the library from ourofficial download site, then you're already set. Otherwise you may have an old version of the AdMob SDK that was released prior to March 15, 2011, and your new app will not receive any ad impressions until you update your SDK.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值