Ref : http://android.jmsliu.com/209/add-google-admob-in-android-application.html
______________________

Admob is a new way to monetize mobile applications. Currently, it supports multi-platform, including iPhone, Android, WebOS, and Flash Lite. Here I will show you how to integrate Admob in your android applications.

First, we need to go to the Admob website to register an account, in order to get the Admod account ID.
Second, let’s go the google code to download the latest Google AdMob Ads SDK for android. You also can get download it from your admob “Sites & Apps” setting page. After we get the AdMob SDK, we need to add the jar in our android application Build Path. Then, you just need to put some simple codes to show your ads in your applications.
1. For Admob, it’s better to add the com.google.ads.AdActivity in your Manifest.xml file. And don’t forget to add the user permission for it. Below is my mainfest.xml example.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jms.AdmobExample"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8"/>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AdmobExample"
                  android:label="@string/app_name">
            <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>

2. The basic class in android applications are composed of View objects. For Admob, we need use AdView class which is provided in the SDK jar file. Actually, it will be put into the main layout file, it’s easy for you to build your own layout. Let’s see my main.xml:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/a"
    />
<com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res/com.jms.AdmobExample"
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ads:adSize="BANNER"
    ads:adUnitId="a14dbba084368db"
/>
</LinearLayout>

3. Add the following code in your res/values/attrs.xml file:


1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="com.google.ads.AdView">
      <attr name="adSize">
          <enum name="BANNER" value="1"/>
          <enum name="IAB_MRECT" value="2"/>
          <enum name="IAB_BANNER" value="3"/>
          <enum name="IAB_LEADERBOARD" value="4"/>
      </attr>
      <attr name="adUnitId" format="string"/>
  </declare-styleable>
</resources>

4. Then, the last step is to add the following in your Activity class:

1
2
3
4
5
AdView adview = (AdView)findViewById(R.id.adView);
AdRequest re = new AdRequest();
re.setTesting(true);
re.setGender(AdRequest.Gender.FEMALE);
adview.loadAd(re);

There are a most important things you need to know:
  • At the first time, the Admob Ads will take 1 or 2 minutes to show.
  • In the simulator, I never try it successfully (As Alan Jay Weiner‘s solution, it maybe because I have two INCs, internet adapter, in my computer. His solution is that disable all other NICs in computer, only leave one INC working which can access the internet, and restart the emulator. Then, it will work properly. Thanks Alan. ). I always get the time out error. I only try it in my HTC Desire, and it really works.
  • Some times, it will show no ads to show. It’s not the problem, because it really doesn’t have ads to show for some situations.
I will put my final apk file for you to try, and also you can download my source code here. Have fun
__________

Download APK File
Download source code