2015-01-04

【Android Studio】置入 AdMob 播廣告 - 橫幅廣告(banner)篇

參考資料 ----
Table 1. Individual APIs and corresponding build.gradle descriptions.
橫幅廣告 II
智慧型橫幅廣告


先下載安裝 Google Repository



開一個新專案,其中 Activity 類型選擇 Blank Activity with Fragment



左上角,由 Android 變更為 Project,並展開專案的結構樹,會看到 build.gradle



雙擊 build.gradle 開啟,看到檔案最末的 dependencies,原本如下 ----


  1. dependencies
  2. {
  3. compile fileTree(dir: 'libs', include: ['*.jar'])
  4. compile 'com.android.support:appcompat-v7:21.0.0'
  5. }
  6.  


更新為 ----

  1. dependencies
  2. {
  3. compile fileTree(dir: 'libs', include: ['*.jar'])
  4. compile 'com.android.support:appcompat-v7:21.0.0'
  5. compile 'com.google.android.gms:play-services-ads:8.1.0'
  6. }


存檔,並按一下 工具列Sync Project with Gradle Files




編輯 AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. package="com.google.android.gms.example.bannerexample" >
  5.  
  6. <!-- 取得可顯示 AdMob 廣告的權限 -->
  7. <uses-permission android:name="android.permission.INTERNET"/>
  8. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  9.  
  10. <application
  11. ...
  12. ... >
  13.  
  14. <!-- 這段 meta-data 為必需 -->
  15. <meta-data
  16. android:name="com.google.android.gms.version"
  17. android:value="@integer/google_play_services_version" />
  18.  
  19. <activity
  20. android:name=".MainActivity"
  21. android:label="@string/app_name" >
  22. <intent-filter>
  23. <action android:name="android.intent.action.MAIN" />
  24.  
  25. <category android:name="android.intent.category.LAUNCHER" />
  26. </intent-filter>
  27. </activity>
  28.  
  29. <!-- AdMob 的 AdActivity -->
  30. <activity
  31. android:name="com.google.android.gms.ads.AdActivity"
  32. android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
  33. android:theme="@android:style/Theme.Translucent" />
  34. </application>
  35.  
  36. </manifest>


開啟 app > src > main > res > values > strings.xml,加入要播的廣告 AdUnitID,這個 IDGoogle 提供給我們測試用的,Google 強烈建議,在 app 開發時期,以這個 ID 進行您程式的測試,驗證您的 app 是否可正常顯示廣告,等到 app 要上架前再換上您的實際 ID,以免觸犯 Google AdMob 的規定。


  1. ...
  2. ...
  3.  
  4. <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
  5.  
  6. ...
  7. ...
  8.  


新增一個 fragment layout


修改內容為如下 ----


  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- 插入 xmlns:ads= ... -->
  4. <RelativeLayout
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. xmlns:ads="http://schemas.android.com/apk/res-auto"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent">
  9.  
  10. <com.google.android.gms.ads.AdView
  11. android:id="@+id/adView"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_centerHorizontal="true"
  15. ads:adSize="SMART_BANNER"
  16. ads:adUnitId="@string/banner_ad_unit_id">
  17. </com.google.android.gms.ads.AdView>
  18.  
  19. </RelativeLayout>


adSize  除了 BANNER 外,另有其他值可填入(參考 橫幅廣告 II)
大小 (寬 x 高)說明 適用裝置 AdSize 常值
320x50 標準橫幅廣告 手機和平板電腦 BANNER
320x100 大横幅 手機和平板電腦 LARGE_BANNER
300x250 IAB 中矩形廣告 手機和平板電腦 MEDIUM_RECTANGLE
468x60 IAB 完整橫幅廣告 平板電腦 FULL_BANNER
728x90 IAB 超級橫幅廣告 平板電腦 LEADERBOARD
查看表格 智慧型橫幅廣告 手機和平板電腦 SMART_BANNER


開啟 MainActivity,增加兩個 import,並在 Placeholder 後面新建一個 AdFragment

  1. ...
  2. ...
  3. import com.google.android.gms.ads.AdRequest;
  4. import com.google.android.gms.ads.AdView;
  5.  
  6. ...
  7. ...
  8.  
  9. public static class PlaceholderFragment extends Fragment
  10. {
  11. ...
  12. ...
  13. }
  14.  
  15. // 播 AdMob 的 fragment
  16. public static class AdFragment extends Fragment
  17. {
  18. @Override
  19. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  20. {
  21. return inflater.inflate(R.layout.fragment_ad, container, false);
  22. }
  23.  
  24. @Override
  25. public void onActivityCreated(Bundle bundle)
  26. {
  27. super.onActivityCreated(bundle);
  28. AdView mAdView = (AdView) getView().findViewById(R.id.adView);
  29. AdRequest adRequest = new AdRequest.Builder().build();
  30. mAdView.loadAd(adRequest);
  31. }
  32. }


修改 activity_main.xml,將原本的 FragmentLayout 變更為 RelativeLayout,並加入 PlaceholderFragmentAdFragment,如下 ----

  1. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/container"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. tools:ignore="MergeRootFrame" >
  9.  
  10. <fragment
  11. android:name="com.google.android.gms.example.bannerexample.MainActivity$PlaceholderFragment"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:layout_above="@+id/adFragment" />
  15.  
  16. <fragment
  17. android:id="@+id/adFragment"
  18. android:name="com.google.android.gms.example.bannerexample.MainActivity$AdFragment"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_alignParentBottom="true" />
  22.  
  23. </RelativeLayout>


修改 MainActivity.javaonCreate(),如下 ----

  1. ...
  2. ...
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState)
  6. {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. }
  10.  


執行程式,就可以看到在畫面下方播放廣告了。



相關筆記 ----
【Android Studio】置入 AdMob 播廣告 - 插頁式廣告(InterstitialAd)篇
【Android】app 置入 Admob 廣告賺錢