Add Google Admob in Android Application

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 = "a14e0c0deb26647"
/>
</ 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. 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 :P

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值