miércoles, 17 de octubre de 2012

Add Google Admob in Android Application


What is Admob?

Admob is a new way to monetize mobile applications. Currently, it supports multi-platform, including iPhone, Android, WebOS, and Flash Lite. In this AdMob for Android example, I will show you how to integrate Admob in your android applications defined in layout XML file.

First of all, we need to go to the Admob website to register an account, in order to get the Admod account ID. Then, 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 AdMob sdk jar in our android project. For those who want to implement the AdMob in Android Java code, you can copy the Jar file in the project and include it in your android project Build path. For more details, please refer the post Add AdMob v6.0 to Android Apps. If you want to layout the AdView in layout xml file, you need to add jar file in the /libs/ folder under your android project.
AdMob Build Path
AdMob Build Path

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.

?
<?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|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </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:
?
<?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/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ads:adSize="BANNER"
    ads:adUnitId="Your Admob ID"
/>
</linearLayout>

3. Send AdMob AdRequest in your Activity class:

AdView adview = (AdView)findViewById(R.id.adView);
AdRequest re = new AdRequest();
re.setTesting(true);
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. 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.
Compile Errors Solutions
One common compiling error is “Error inflating class com.google.ads.AdView”. The error message is:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jms/com.jms.InfiniteGalleryActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class com.google.ads.AdView
This is one of the compile errors which many android programmers meet with. The solution is creating the /libs folder under your android project root folder and putting the AdMob Jar file inside.

AdMob Build Path

Another compiling error is “Required XML attribute missing”. The error message is:
Could not initialize AdView: Required XML attribute “adSize” missing
This is because the Android can not find the AdSize defination. It can be solved by specifying the right ads name spacing in the layout xml:
?
xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"

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

No hay comentarios:

Publicar un comentario