例子工程源码下载地址: 下载源代码
固然,我也参考了一些网上的资料,主要有:php
AdMob:在android应用中嵌入广告的方案
如何在Android Market赚钱 part 2 - 免费app附带广告
Publisher Starter Kit
面向开发者 Wiki 的 AdMobhtml
好了,如今让我从头开始提及……在这以前,你不须要有任何的账号,惟一须要的就是有一个有效的email邮箱。只要按照下面的步骤一步步来,你就能经过将Admob的广告插到本身的程序中赚钱啦!java
首先,固然是须要注册一个Admob的账号。Admob的主页是:http://www.admob.com/ 。 固然,若是你对于浏览英文网页还有些障碍的话,能够登陆中文网站:http://zhcn.admob.com/ 。若是网站的文字仍是英文,你能够在网站主页的右下角的“Language”处,选择“中文(简体)”。点击进入注册页面后,有一些栏目须要填写,不要太过疑虑,就像你注册一个论坛同样,随便填下就行了。最关键的是保证填写的email地址有效,另外就是填上姓名,选择语言。账户类型我选择的“不肯定”,语言“中文(简体)”~ 提交注册申请以后,不久你就会收到用于确认并激活账号的电子邮件,点击激活连接,就能够了激活你的Admob账号了~android
第二步就是设置你的Android应用程序信息,并得到Admob的插入代码。登陆你的Admob账号后,在主页的左上方(Logo上面)点击“Marketplace(手机广告市场)”,进入页面后,在“Sites&Apps(站点和应用程序)”标签下,点击“Add Site/App”。选择咱们熟悉的图标——" Android App ” 。这时会出现须要你填写一个“详细信息”,随便填上一些信息。(不要太过在乎如今填写的东西,由于这些之后都是能够修改的)。好比“Android Package URL” 我到如今都尚未填写,描述之类的,想写就写点吧。填好详细信息后,点击“继续”,就能够到AdMob Android SDK 的下载页面了。下载这个SDK(固然,这个很重要)。app
The AdMob Android SDK includes:网站
README: Get started with AdMob Android ads!
AdMob Jar file: Required for publishing ads. Follow the documentation in javadoc/index.html and drop the AdMob Jar file into your project.
Sample Projects: Examples of AdMob Android ads shown in the LunarLander application.ui
第三步获取你的应用程序对应的Publisher ID。在下载页面点击"Go to Sites/Apps"就能够到你应用程序的管理界面了。这时你会发如今这个页面醒目的位置会有一个叫你填写详细信息的提示:this
在咱们发送任何有待收入以前,您须要填写技术联系详细信息和付款首选项。
咱们暂时能够不用管它,由于钱是会存在咱们的Admob的帐户上的,等咱们须要提现的时候,或者你想填的时候再填就能够了。在下面的列表中,选择你的应用程序并进入。这个界面就是你的应用程序广告的管理界面了,里面有比较多的功能,之后能够慢慢了解,如今咱们只须要知道两个东西,一个是发布者 ID(Publisher ID),一个是你程序的状态。Publisher ID是一个15个字符的字符串,而你程序的状态如今应该仍是不活动(Inactive)。咱们下面要作的就是怎么让它变为Active。spa
第四步代码编写——在你的应用程序中插入Admob广告。通过上面的步骤,咱们在网站上的设置就告一个段落了,如今咱们终于要进入主题了,如何在本身的Android应用程序中插入Admob广告。若是你不健忘的话,必定还记得咱们以前下载的那个AdMob Android SDK 。解压它,看看里面有些什么东西。这里面最重要的就是那个名为“admob-sdk-android.jar”的包啦,Admob将如何把广告加载到Android应用程序中的代码集成在这个包里,咱们编写程序的时候就须要将这个包导入到咱们的工程里面去。另外,解压出来的文件夹中还有一个名为“javadoc”的文件夹,打开它里面的index.html。它是关于Admob Android SDK的帮助文档,在Package 下的Setup下,有详细完整的在本身的应用程序中插入广告的方法介绍,在这里我就偷懒,引用一下~
Including the Jar
Add the Jar file included with the SDK to your Android project as an external library. In your project's root directory create a subdirectory libs (this will already be done for you if you used Android's activitycreator). Copy the AdMob Jar file into that directory. For Eclipse projects:.net
Go to the Properties of your project (right-click on your project from the Package Explorer tab and select Properties)
Select "Java Build Path" from left panel
Select "Libraries" tab from the main window
Click on "Add JARs..."
Select the JAR copied to the libs directory
Click "OK" to add the SDK to your android project
注意:须要首先在你工程的根目录下新建一个叫作“libs”的文件夹,并把以前所说的最重要的东西“admob-sdk-android.jar”复制到里面。
AndroidManifest.xml
Your AdMob publisher ID was given to you when creating your publisher account on www.admob.com before downloading this code. It is a 15-character code like a1496ced2842262. Just before the closing </application> tag add a line to set your publisher ID:
<!-- The application's publisher ID assigned by AdMob -->
<meta-data android:value="YOUR_ID_HERE" android:name="ADMOB_PUBLISHER_ID" />
</application>
Set any permissions not already included just before the closing </manifest> tag:
<!-- AdMob SDK permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
Only the INTERNET permission is required. Setting ACCESS_COARSE_LOCATION (and/or ACCESS_FINE_LOCATION) allows narrowly geo-targeted ads be shown.
这里须要注意的是,<meta-data android:value="YOUR_ID_HERE" android:name="ADMOB_PUBLISHER_ID" />中,咱们只须要改的是"YOUR_ID_HERE"。这里须要你填上的ID就是咱们以前在Admob网站咱们的应用程序管理页面上看到的Publisher ID,而name="ADMOB_PUBLISHER_ID"是不该该改的。程序须要这个Key来查找对应的Value。
attrs.xml
The attrs.xml file specifies custom AdView attributes in XML layout files. If your application does not already have an /res/values/attrs.xml file then create one and copy-and-paste the following into it. If you do have that file then just add the declare-styleable element:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.admob.android.ads.AdView">
<attr name="testing" format="boolean" />
<attr name="backgroundColor" format="color" />
<attr name="textColor" format="color" />
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" />
<attr name="isGoneWithoutAd" format="boolean" />
</declare-styleable>
</resources>
这个,没什么说的。
Placing an AdView in a Layout
AdView widgets can be put into any XML layout now. The first step is to reference attrs.xml in your layout element by adding an xmlns line that includes your package name specified in AndroidManifest.xml:
xmlns:yourapp=http://schemas.android.com/apk/res/yourpackage
For example a simple screen with only an ad on it would look like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:admobsdk="http://schemas.android.com/apk/res/com.admob.android.example"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.admob.android.ads.AdView
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
admobsdk:backgroundColor="#000000"
admobsdk:textColor="#FFFFFF"
admobsdk:keywords="Android application"
/>
</LinearLayout>
这里好像也没什么须要特别注意的,注意加上xmlns,另外知道这里能够设置一个keywords。
Test Mode
When you start integrating AdMob ads into your application it is recommended to use test mode. This always returns the same ad. Normal ad requests are not deterministic making it harder to be sure the ad view looks like you want (e.g. ad requests can timeout or may not fill).
Once the ad shows up as you expect be sure to turn test mode off to get real ads. Never put your application into the Android Market with test mode enabled.
Test mode can be enabled either by calling AdManager.setInTestMode(true) or by adding a "admobsdk:testing="true"" property to the ad in your XML layout (where "admobsdk" is the XML namespace for your application).
设置Test Mode这个很关键,千万别将处于Test Mode的程序发布出去了,那样可赚不了钱啊~!若是在AdView的属性中不加上admobsdk:testing="false",彷佛程序也是不处于Test Mode的,不过最好仍是加上吧~
第五步编译运行,并激活程序。编译运行你的程序,在模拟器上就能够看到效果啦~(固然你的模拟器须要能上网,关于怎么让模拟器上网呢?用路由的应该能够直接上,若是不是用路由,那么可能须要设置下,具体方法你们本身网上搜吧,具体忘了)。若是你的应用程序能显示出广告,那么恭喜你,你的应用程序很快就会在Admob上被激活了(须要必定的时间,个人好像花了一个小时不到)!
第六步在Admob网站上查看应用程序赚了多少钱~“手机广告市场”—>“报告”—>“站点和应用程序报告”。选择你的应用程序,而后点击页面最下面的“建立报告”~ OK,赚钱啦~
最后,我把我本身写的一个例子工程上传上来,你们能够下载来参考下。另外,个人工程将广告做为移动的,而且改变了它默认的宽度和背景,但愿对如何在应用程序中摆放广告,起到一个抛砖引玉的做用。效果图以下(哈哈,在模拟器跑的~):
例子工程源码下载地址: 下载源代码