また、アプリのプロジェクトにgoogle-play-servicesが既に組み込まれているものとします。
ここまでの詳しい手順は、
がとても参考になります。
ここまでの条件が整っているという過程のもと、Activityに広告を表示するには対象ActivityのonCreate内で以下のようにします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AdView adView = new AdView(this); | |
adView.setAdUnitId([AdMobから通知される広告ユニット ID]); | |
adView.setAdSize(AdSize.BANNER); | |
// id = ad の LinearLayoutがあるとして、そこに広告を表示する | |
LinearLayout headLayout = (LinearLayout) findViewById(R.id.ad); | |
headLayout.addView(adView); | |
AdRequest adRequest = new AdRequest.Builder().build(); | |
// 広告をテストモードで表示するなら | |
// AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice([ハッシュ化された端末 ID]).build(); | |
adView.loadAd(adRequest); |
[Android] AdMob テストで使う、デバイスIDを取得
のエントリーに書かれている方法を使うと、デバイス切り替えごとにIDを書き換えずに済んで素敵です。
0 件のコメント:
コメントを投稿