Monday, June 18, 2012

AdMob Manager In Android Application



This is a sample activity which shows how to use AdMob Manager in your application.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Register or login at admob.com, and download GoogleAdMobAdsSdkAndroid-4.0.4, which contains a GoogleAdMobAdsSdk.jar file.
2.) Create a project MyAdMob.
3.) In your project’s root directory create a subdirectory lib. Copy the AdMob JAR (GoogleAdMobAdsSdk.jar) file into that lib directory.

4.) Right-click on your project from the Package Explorer tab and select Properties.
5.) Select Java Build Path from left panel and Select Libraries tab from the main window.
6.) Click on Add JARs.
7.) Select the JAR copied to the libs directory, Click OK to add the SDK to your Android project.
8.) Add some relevant permissions in AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.sample.MyAdMob" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
   <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyAdMob" 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>
9.) Run the Application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MyAdMob. Enter following information:
Project name: MyAdMob
Build Target: Android 2.1
Application name: MyAdMob
Package name: com.sample.MyAdMob
Create Activity: MyAdMob
On Clicking Finish MyAdMob code structure is generated with the necessary Android Packages being imported along with MyAdMob.java. MyAdMob class will look like following:
package com.sample.MyAdMob;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MyAdMob extends Activity
{
    private static final String MY_BANNER_UNIT_ID = "a14db01ee16f245";
 
   @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Lookup R.layout.main
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
        // Create the adView
        // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
        AdView adView = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);
        // Add the adView to it
        layout.addView(adView);
        // Initiate a generic request to load it with an ad
        AdRequest request = new AdRequest();
        request.setTesting(true);
        adView.loadAd(request);          
   }
}
Output –The final output:

No comments:

Post a Comment