例子工程源碼下載地址: 下載源代碼
固然,我也參考了一些網上的資料,主要有: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,賺錢啦~
最後,我把我本身寫的一個例子工程上傳上來,你們能夠下載來參考下。另外,個人工程將廣告做爲移動的,而且改變了它默認的寬度和背景,但願對如何在應用程序中擺放廣告,起到一個拋磚引玉的做用。效果圖以下(哈哈,在模擬器跑的~):
例子工程源碼下載地址: 下載源代碼