|
1.將下載的AdMob SDK解壓縮,將該SDK的JAR檔案路徑加入到應用程式內
2.manifest檔案內設定INTERNET與ACCESS_NETWORK_STATE uses permissions,並宣告com.google.ads.AdActivity與設定configChanges屬性- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:name="com.google.ads.AdActivity"></activity>
複製代碼 3.在layout檔案內新增AdView元件
adSize:設定廣告看板的尺寸
adUnitId:填入發佈商ID。
loadAdOnCreate:是否一開始就載入廣告。
testDevices:設定測試用的裝置;如果是實機則必須輸入實機的ID,就是IMEI碼- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Admob廣告輪播"
- android:textSize="20sp" />
- <com.google.ads.AdView
- android:id="@+id/adView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- ads:adSize="BANNER"
- ads:adUnitId="a1521ffbba6766b"
- ads:loadAdOnCreate="true"
- ads:testDevices="TEST_EMULATOR" />
- </LinearLayout>
複製代碼 4.在程式中加上- AdView adView= (AdView)findViewById(R.id.adView);
- adView.loadAd(new AdRequest());
複製代碼 |
|