你可以到這邊申請帳號 AdMob ,登入後可以選擇透過新應用程式盈利
手動新增應用程式,輸入並選擇應用程式種類。
接著選擇廣告的格式,有分橫幅廣告或插頁式廣告,橫幅就是一條廣告,而插頁式是全版廣告,這邊先選擇橫幅廣告。
可以設定重新整理的頻率,以及這則廣告的命名。
按下確認後,你會看到你的廣告單元編號,像是 ca-app-pub-一串數字。
這串編號就是代表你這個廣告的代碼,只要透過這個代碼就能去跟AdMob要廣告,
而使用者點選或看到這則廣告,你就有收益。
此時帳戶的設定差不多完成了,試著把這個廣告加入到你的App之中。
首先先下載Google Repository,點選SDK Manager,
安裝Extras裡面的Google Repository
接著選擇Gradle Scripts裡面的build.gradle(app)
在dependencies加入以下這行
compile 'com.google.android.gms:play-services:6.+'
看到黃色底可能會覺得有些不安,不過沒關係可以不用理他。
在AndroidManifest.xml加入以下三個東西
1. 存取網路的權限,因為廣告需要用到網路
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2.Android 可藉此瞭解應用程式預期要用的服務版本。
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
3.有人按下橫幅廣告或出現插頁廣告時,SDK 才會使用這個活動,而且與其他活動一樣,必須先在資訊清單中宣告後才能顯示。
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />
這些程式碼要放哪可以參考這張圖
這時候有些紅字該怎麼解決,你只需要按Run即可,因為我們剛才有設定Gradle,他會自動幫我們導入需要的東西,只是可能需要一些時間。
當紅字去除時,就可以加入廣告了。
首先先到Strings.xml加入廣告的key,也就是上面網頁申請的廣告代碼。
若是沒有你也可以先使用Google提供的範例Key
ca-app-pub-3940256099942544/6300978111
像是這個樣子
接著到你要顯示的頁面加入這個xml語法,事實上它是一個custom view。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<com.google.android.gms.ads.AdView | |
android:id="@+id/adView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_alignParentBottom="true" | |
ads:adSize="BANNER" | |
ads:adUnitId="@string/banner_ad_unit_id"> | |
</com.google.android.gms.ads.AdView> |
此時你會看到ads那部分是紅字,你可以像是在寫程式一樣使用快捷鍵讓系統幫你自動產生需要的程式碼(alt + enter),或者你手動加入這行語法。
xmlns:ads="http://schemas.android.com/apk/res-auto"
接著載入廣告,你只需要很簡單的寫這幾行即可。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AdView mAdView = (AdView) findViewById(R.id.adView); | |
AdRequest adRequest = new AdRequest.Builder().build(); | |
mAdView.loadAd(adRequest); |
你可以看一下你的LogCat,搜尋add,他會告訴你請加入測試設備代碼。
意思是,把你這台設備加入到測試之中,這樣只會看到測試的廣告,不會違反條例。
把上面的程式碼稍微修改一下,會變成這個樣子。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AdView mAdView = (AdView) findViewById(R.id.adView); | |
AdRequest adRequest = new AdRequest.Builder().addTestDevice("428B768F8DCD668A917CCCFBC14EE833").build(); | |
//加入你的設備代碼 | |
mAdView.loadAd(adRequest); |
這時候你看到的廣告應該會是這個樣子
一般看到的廣告都是等同螢幕寬,那要怎設定呢,你只需要把上面的XML程式碼之中的,
ads:adSize稍作修改即可,改成SMART_BANNER,他會隨著螢幕大小而作改變,而唯一不變的是會填滿寬。
改完後,卻發現廣告跳不出來了,而又跑出Not enough space to show ad.的錯誤代碼,
是因為他必須等同螢幕寬,但是RelativeLayout一開始可能會自動幫你設定縮排,也就是padding,我們把左右縮排都去掉,就可以了。
去掉這兩個
此時,這樣的廣告效果,看起來是不是不錯呢。
還有其他的橫幅寬高,這裏就不多作介紹,可以參考Google的說明
沒有留言:
張貼留言