アプリ作って海外移住

コピペプログラマーのメモ

Google Play Services SDK AdMob広告

google play service lib インポート

ここにある!

<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/

これを指定してインポートする。

 

プロジェクトを右クリックしてプロパティをクリック。

ライブラリーにgoogle-play-services_libが追加されているのを確認して、OK

 

 

 

②マニュフェト修正

<?xml version="1.0"encoding="utf-8"?>
    package="com.example.testadmob"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity
 
            android:name="com.example.testadmob.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.android.gms.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>

③コード

 
<LinearLayout
android:id="@+id/layout_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical" >
 
 
 
 
 
 
 //////////////////////////////////////admob
private LinearLayout layout_ad;//広告表示用スペース
private AdView adView;

 
oncreate に記述
///////////////////////////////////////////////////////////admob
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-7760709895253433/7403533301");
adView.setAdSize(AdSize.BANNER);

layout_ad = (LinearLayout) findViewById(R.id.layout_ad);
layout_ad.addView(adView);

//本番
AdRequest adRequest = new AdRequest.Builder().build();

//テスト
// AdRequest adRequest = new AdRequest.Builder()
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // エミュレータ
// .addTestDevice("xxxxxxxxxxxxxxxxxxx") // Galaxy Nexus テスト用携帯電話
// .build();

adView.loadAd(adRequest);
//////////////////////////////////////////////////////////////admob////////
 
 
 
 
 
 
 
 
 
以下では落ちる?
 
 
 AdView adView=(AdView)this.findViewById(R.id.adView);
    // 一般モード
    AdRequest adRequest = new AdRequest.Builder().build();
 
    // テストモード
    AdRequest adRequest=newAdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)    // エミュレータ
    .addTestDevice(MobileID)// テストデバイスなんでもOK
    .build();
    adView.loadAd(adRequest);
 
②レイアウトXML
 
 
 
 
 
 
 
 
 
<com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-xxxxxxxxxx/nnnnnnnnnn" />//これはadmobで取得