2013年9月23日 星期一

[Android] 如何幫自己的App加上廣告

由於最近剛幫自己的Android App放上廣告,藉這個機會這邊跟大家分享一下自己的心得。開始之前,首先你必須為自己註冊一個Google AdMob帳戶並取得SDK,接著我們就可以開始將廣告放入App之中了,Android的廣告其實就是在App的介面中嵌入一個"AdView",而嵌入的方式除了可以用Java撰寫外亦可透過XML來達到相同目的,在此我要教大家的便是藉由xml的方式來放置Google廣告,既簡單又便利。
 
要在應用程式中加入Google廣告,我們必須完成三個步驟:
 
  1. 在Eclipse專案中加入SDK JAR檔。
  2. AndroidManifest.xml中宣告com.google.ads.AdActivity及設定必要的網路權限。
  3. 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開通INTERNETACCESS_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();
}

 

結論

透過上述的步驟我們就可以輕鬆的將Google廣告放至於App上任何介面之中,最後的呈現結果如畫面中的所看到的行幅廣告,其中我所使用的裝置版本為Android 4.2.2:
 

沒有留言:

張貼留言