花了整整一天時(shí)間,研究怎么通過(guò)在Android應(yīng)用程序中添加Admob廣告來(lái)賺錢。網(wǎng)上也有些教程,不過(guò)說(shuō)得不夠詳細(xì),自己還得花時(shí)間摸索。為了讓后來(lái)者能更快開始Admob廣告賺錢,所以寫個(gè)詳細(xì)一點(diǎn)的教程,供大家參考。
例子工程源碼下載地址:
下載源代碼
當(dāng)然,我也參考了一些網(wǎng)上的資料,主要有:
AdMob:在android應(yīng)用中嵌入廣告的方案
如何在Android Market賺錢 part 2 - 免費(fèi)app附帶廣告
Publisher Starter Kit
面向開發(fā)者 Wiki 的 AdMob
好了,現(xiàn)在讓我從頭開始說(shuō)起……在這之前,你不需要有任何的帳號(hào),唯一需要的就是有一個(gè)有效的email郵箱。只要按照下面的步驟一步步來(lái),你就能通過(guò)將Admob的廣告插到自己的程序中賺錢啦!
首先,當(dāng)然是需要注冊(cè)一個(gè)Admob的帳號(hào)。Admob的主頁(yè)是:http://www.admob.com/ 。 當(dāng)然,如果你對(duì)于瀏覽英文網(wǎng)頁(yè)還有些障礙的話,可以登錄中文網(wǎng)站:http://zhcn.admob.com/ 。如果網(wǎng)站的文字還是英文,你可以在網(wǎng)站主頁(yè)的右下角的“Language”處,選擇“中文(簡(jiǎn)體)”。點(diǎn)擊進(jìn)入注冊(cè)頁(yè)面后,有一些欄目需要填寫,不要太過(guò)疑慮,就像你注冊(cè)一個(gè)論壇一樣,隨便填下就好了。最關(guān)鍵的是保證填寫的email地址有效,另外就是填上姓名,選擇語(yǔ)言。帳戶類型我選擇的“不確定”,語(yǔ)言“中文(簡(jiǎn)體)”~ 提交注冊(cè)申請(qǐng)之后,不久你就會(huì)收到用于確認(rèn)并激活帳號(hào)的電子郵件,點(diǎn)擊激活鏈接,就可以了激活你的Admob帳號(hào)了~
第二步就是設(shè)置你的Android應(yīng)用程序信息,并獲得Admob的插入代碼。登錄你的Admob帳號(hào)后,在主頁(yè)的左上方(Logo上面)點(diǎn)擊“Marketplace(手機(jī)廣告市場(chǎng))”,進(jìn)入頁(yè)面后,在“Sites&Apps(站點(diǎn)和應(yīng)用程序)”標(biāo)簽下,點(diǎn)擊“Add Site/App”。選擇我們熟悉的圖標(biāo)——" Android App ” 。這時(shí)會(huì)出現(xiàn)需要你填寫一個(gè)“詳細(xì)信息”,隨便填上一些信息。(不要太過(guò)在意現(xiàn)在填寫的東西,因?yàn)檫@些以后都是可以修改的)。比如“Android Package URL” 我到現(xiàn)在都還沒(méi)有填寫,描述之類的,想寫就寫點(diǎn)吧。填好詳細(xì)信息后,點(diǎn)擊“繼續(xù)”,就可以到AdMob Android SDK 的下載頁(yè)面了。下載這個(gè)SDK(當(dāng)然,這個(gè)很重要)。
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.
第三步獲取你的應(yīng)用程序?qū)?yīng)的Publisher ID。在下載頁(yè)面點(diǎn)擊"Go to Sites/Apps"就可以到你應(yīng)用程序的管理界面了。這時(shí)你會(huì)發(fā)現(xiàn)在這個(gè)頁(yè)面醒目的位置會(huì)有一個(gè)叫你填寫詳細(xì)信息的提示:
在我們發(fā)送任何有待收入之前,您需要填寫技術(shù)聯(lián)系詳細(xì)信息和付款首選項(xiàng)。
我們暫時(shí)可以不用管它,因?yàn)殄X是會(huì)存在我們的Admob的賬戶上的,等我們需要提現(xiàn)的時(shí)候,或者你想填的時(shí)候再填就可以了。在下面的列表中,選擇你的應(yīng)用程序并進(jìn)入。這個(gè)界面就是你的應(yīng)用程序廣告的管理界面了,里面有比較多的功能,以后可以慢慢了解,現(xiàn)在我們只需要知道兩個(gè)東西,一個(gè)是發(fā)布者 ID(Publisher ID),一個(gè)是你程序的狀態(tài)。Publisher ID是一個(gè)15個(gè)字符的字符串,而你程序的狀態(tài)現(xiàn)在應(yīng)該還是不活動(dòng)(Inactive)。我們下面要做的就是怎么讓它變?yōu)锳ctive。
第四步代碼編寫——在你的應(yīng)用程序中插入Admob廣告。經(jīng)過(guò)上面的步驟,我們?cè)诰W(wǎng)站上的設(shè)置就告一個(gè)段落了,現(xiàn)在我們終于要進(jìn)入主題了,如何在自己的Android應(yīng)用程序中插入Admob廣告。如果你不健忘的話,一定還記得我們之前下載的那個(gè)AdMob Android SDK 。解壓它,看看里面有些什么東西。這里面最重要的就是那個(gè)名為“admob-sdk-android.jar”的包啦,Admob將如何把廣告加載到Android應(yīng)用程序中的代碼集成在這個(gè)包里,我們編寫程序的時(shí)候就需要將這個(gè)包導(dǎo)入到我們的工程里面去。另外,解壓出來(lái)的文件夾中還有一個(gè)名為“javadoc”的文件夾,打開它里面的index.html。它是關(guān)于Admob Android SDK的幫助文檔,在Package 下的Setup下,有詳細(xì)完整的在自己的應(yīng)用程序中插入廣告的方法介紹,在這里我就偷懶,引用一下~
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:
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
注意:需要首先在你工程的根目錄下新建一個(gè)叫做“libs”的文件夾,并把之前所說(shuō)的最重要的東西“admob-sdk-android.jar”復(fù)制到里面。
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網(wǎng)站我們的應(yīng)用程序管理頁(yè)面上看到的Publisher ID,而name="ADMOB_PUBLISHER_ID"是不應(yīng)該改的。程序需要這個(gè)Key來(lái)查找對(duì)應(yīng)的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>
這個(gè),沒(méi)什么說(shuō)的。
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>
這里好像也沒(méi)什么需要特別注意的,注意加上xmlns,另外知道這里可以設(shè)置一個(gè)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).
設(shè)置Test Mode這個(gè)很關(guān)鍵,千萬(wàn)別將處于Test Mode的程序發(fā)布出去了,那樣可賺不了錢啊~!如果在AdView的屬性中不加上admobsdk:testing="false",似乎程序也是不處于Test Mode的,不過(guò)最好還是加上吧~
第五步編譯運(yùn)行,并激活程序。編譯運(yùn)行你的程序,在模擬器上就可以看到效果啦~(當(dāng)然你的模擬器需要能上網(wǎng),關(guān)于怎么讓模擬器上網(wǎng)呢?用路由的應(yīng)該可以直接上,如果不是用路由,那么可能需要設(shè)置下,具體方法大家自己網(wǎng)上搜吧,具體忘了)。如果你的應(yīng)用程序能顯示出廣告,那么恭喜你,你的應(yīng)用程序很快就會(huì)在Admob上被激活了(需要一定的時(shí)間,我的好像花了一個(gè)小時(shí)不到)!
第六步在Admob網(wǎng)站上查看應(yīng)用程序賺了多少錢~“手機(jī)廣告市場(chǎng)”—>“報(bào)告”—>“站點(diǎn)和應(yīng)用程序報(bào)告”。選擇你的應(yīng)用程序,然后點(diǎn)擊頁(yè)面最下面的“創(chuàng)建報(bào)告”~ OK,賺錢啦~
最后,我把我自己寫的一個(gè)例子工程上傳上來(lái),大家可以下載來(lái)參考下。另外,我的工程將廣告作為移動(dòng)的,并且改變了它默認(rèn)的寬度和背景,希望對(duì)如何在應(yīng)用程序中擺放廣告,起到一個(gè)拋磚引玉的作用。效果圖如下(哈哈,在模擬器跑的~):