2010年11月10日水曜日

AdMob に挑戦!

AdMob とは、モバイル向けの広告サービス(Google AdSense のモバイル版)です。

ということを本日知りました(^_^;)アセアセ

・・・ということで早速試してみることにしました!



しか~し!
当初、以下のサイトを見ていたのですが、どうも情報が古いようで。。

http://developer.admob.com/wiki/Android

上記サイト通りにやると、以下のようなエラーが LogCat に記録されて、
ちっとも広告が表示されませんでした。シクシク。

could not find com.admob.android.ads.AdMobActivity, please make sure it is registered in AndroidManifest.xml
com.admob.android.ads.AdMobActivity must be registered in your AndroidManifest.xml file.


ということで、、

以下のPDFを参考にして作業をすれば、無事、広告表示までサンプル・アプリを作成できました。
参考PDF:
http://www.admob.com/docs/AdMob_Android_SDK_Instructions.pdf


以下に、手順をメモしておきます。

  1. AdMob のアカウントを作成。

    AdMob サイト より、"未登録の方は、今すぐ登録する" を選択します。

    すると、アカウント情報を入力する画面になるので、太字になっている箇所を入力します。

    入力が終わると、アカウント情報で指定したメールアドレスにメールが配信されます。
    配信されたメールに、アカウントを有効化にするリンクがあるので、このリンクをクリックします。

    以上で、アカウント作成は完了です。


  2. AdMob SDK 入手

    作成したアカウントでログインします。

    ログイン後、"サイト及びアプリケーション" のタブを選択し、
    "+サイト/アプリケーションの追加" をクリックします。



    アプリケーションのタイプとして、"Android アプリケーション" を選択します。
    App名、ジャンル、Appの説明の 3 つは入力が必須なので、適当に入力します。
    Android パッケージURL は必須ではないので、特になければ空白にしておきます。



     アプリケーションの登録が完了後、
    "AdMob Android SDK のダウンロード" をクリックし、SDK をダウンロードします。



  3. AdMob サンプル・アプリの作成 (プロジェクト作成~ライブラリ追加)

    android プロジェクトを作成します。
    とりあえず、以下の感じで作成してみました。

    項目
    Project name AdMobTest Proj
    Build Target Android 1.6
    Application name   AdMobTest
    Package name jp.kochi.AdMobTest   
    Create Activity AdMobTestActivity
    Min SDK Version 4


    プロジェクトを作成したら、libs という名前でフォルダを作成します。

    libs フォルダを作成したら、その中に、admob-sdk-android.jar をコピー(Import)します。

    次に、プロジェクトの Properties を開き、"Java Build Path" を選択し、
    "Libraries" から "Add JARs..." を実行して、libs フォルダにコピーした admob-sdk-android.jar を選びます。




  4. AdMob サンプル・アプリの作成 (パブリッシャーIDの登録~アプリ完成/起動確認)

    マニフェスト・ファイルに修正(青字部分が追記部分)します。

    AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="jp.kochi.AdMobTest" android:versionCode="1"
      android:versionName="1.0">
      <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AdMobTestActivity" android:label="@string/app_name">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
        </activity>

        <!-- The application's publisher ID assigned by AdMob -->
        <meta-data android:value="a14xxxxxxxxxxxx" android:name="ADMOB_PUBLISHER_ID" />

        <!-- AdMobActivity definition -->
        <activity android:name="com.admob.android.ads.AdMobActivity"
          android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
          android:configChanges="orientation|keyboard|keyboardHidden" />
        <!-- Track Market installs -->
        <receiver android:name="com.admob.android.ads.analytics.InstallReceiver"
          android:exported="true">
          <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
          </intent-filter>
        </receiver>

        <meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" />

      </application>
      <uses-sdk android:minSdkVersion="4" />

      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
      <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    </manifest> 

    meta-data android:value="a14xxxxxxxxxxxx" で指定するパブリッシャーIDは、
    AdMob のサイトにて、"サイト及びアプリケーション" から該当アプリの "設定管理" より確認できます。



    res/values フォルダに、attr.xml を新規作成し、以下の内容を記述します。

    res/values/attr.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <declare-styleable name="com.admob.android.ads.AdView">
        <attr name="backgroundColor" format="color" />
        <attr name="primaryTextColor" format="color" />
        <attr name="secondaryTextColor" format="color" />
        <attr name="keywords" format="string" />
        <attr name="refreshInterval" format="integer" />
      </declare-styleable>
    </resources>


    res/layout/main.xml に以下の内容を記述します。
    xmlns:myapp="http://schemas.android.com/apk/res/<パッケージ名>" の <パッケージ名> には、
    マニフェスト・ファイルに記述されているパッケージ名を指定します。

    res/layout/main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:myapp="http://schemas.android.com/apk/res/jp.kochi.AdMobTest"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <com.admob.android.ads.AdView
        android:id="@+id/ad"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        myapp:backgroundColor="#000000"
        myapp:primaryTextColor="#FFFFFF"
        myapp:secondaryTextColor="#CCCCCC" />
    </LinearLayout>


    最後に Activity です。
    エミューレーターで動作確認をする場合、
    以下のように、テストモードに設定する必要があるようです。

    AdMobTestActivity.java
    public class AdMobTestActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            AdManager.setTestDevices(new String[] { AdManager.TEST_EMULATOR });

            AdView adView = (AdView)findViewById(R.id.ad);
            adView.requestFreshAd();

        }
    }

    以上でサンプル・アプリ作成は完了です。

    ちなみに、実機で稼動させると、LogCat 上に以下のようなメッセージ(デバイスID)が表示されます。

    I/AdMobSDK( 1712): To get test ads on this device use AdManager.setTestDevices(new String[] { "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" } )

    エミュレータに加え、実機でもテスト・モードで稼動させるには、
    この xxxxx のデバイスIDも、AdManager.setTestDevices() で指定すればよいです。

    AdManager.setTestDevices(new String[] { AdManager.TEST_EMULATOR, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" });


    エミュレータで稼動させると、1回目はうまく表示されませんでしたが、
    2回目は、きちんと広告が表示されました~


何だか、最後まで、すんなりといきませんでしたが、
とりあえずヨシとしておきます。