要在應用程式中加入Google廣告,我們必須完成三個步驟:
- 在Eclipse專案中加入SDK JAR檔。
- 在AndroidManifest.xml中宣告com.google.ads.AdActivity及設定必要的網路權限。
- 在res/layout/main.xml中定義AdView物件,並使用ads:loadAdOnCreate屬性將廣告載入,或者透過Java程式將廣告放至於App介面之中。
新增SDK JAR檔
解壓縮SDK後會看到一個JAR檔案、javadoc資料夾、README文件,在此我們要將JAR匯入至Eclipse專案之中。1. 按右鍵點選"Properties" | ![]() |
2. 點選"Java Build Path"選擇"Libraries > Add External JARs..",將JAR匯入專案程式庫中。 | ![]() |
3. 切換至"Order and Export"勾選Google AdMob Ads JAR,當執行程式於將會一同Compiling於專案中。 | ![]() |
宣告com.google.ads.AdActivity
使用AdMob Ads SDK前我們必須於在AndroidManifest.xml中宣告com.google.ads.AdActivity:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company"
android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:label="@string/app_name" android:name="BannerExample">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
使用權限
使用廣告時也必須在AndroidManifest.xml開通INTERNET和ACCESS_NETWORK_STATE兩種權限:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company"
android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:label="@string/app_name" android:name="BannerExample">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
定義com.google.ads.AdView
於res/layout/main.xml中新增xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"與定義AdView物件,並透過ads:loadAdOnCreate="true"將廣告載入:<?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:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.google.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adUnitId="MY_AD_UNIT_ID" ads:adSize="BANNER" ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" ads:loadAdOnCreate="true"/> </LinearLayout>其中"MY_AD_UNIT_ID"為你申請AdMob時所提供的發佈商編號,而ads:testDevices為自己的裝置編號,這部份可以從Android開發工具中的LogCat得知,如下圖:
經由Java程式載入廣告
如果想要在應用程式中載入廣告,可以從上述程式碼中移除ads:loadAdOnCreate="true",改為透過findViewById查詢Adview,接著使用loadAd的方式來載入廣告:import com.google.ads.*;
public class GoogleAdExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 查詢 AdView 這項物件,並載入請求。
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
}
@Override
public void onDestroy() {
if(AdView != null){
AdView.destroy();
}
super.onDestroy();
}
沒有留言:
張貼留言