AdMob インタースティシャル広告は、いままでおなじみのバナーではなく全画面の広告です。ちょっといい感じの無料ゲームを落としてみるとこの Interstitial 広告に当たります、最初は広告かどうか分からずに思わずタップしてしまいますが…
![[Android] AdMob インタースティシャルの設置 admobinterstitial 4 170x300 - [Android] AdMob インタースティシャルの設置](https://akira-watson.com/wp-content/uploads/2014/07/admobinterstitial_4-170x300.png)
![[Android] AdMob インタースティシャルの設置 admobinterstitial 5 170x300 - [Android] AdMob インタースティシャルの設置](https://akira-watson.com/wp-content/uploads/2014/07/admobinterstitial_5-170x300.png)
![[Android] AdMob インタースティシャルの設置 admobinterstitial 6 170x300 - [Android] AdMob インタースティシャルの設置](https://akira-watson.com/wp-content/uploads/2014/07/admobinterstitial_6-170x300.png)
API 29
AdMob Interstitial
まず最初に、Android Studioで新しくプロジェクトを作成する途中段階でこの広告を設定する(Google AdMob Ads Activity)という箇所があります。それでつくると既に設定されたものが出来上がっています。
![[Android] AdMob インタースティシャルの設置 admobinterstitial 1 170x300 - [Android] AdMob インタースティシャルの設置](https://akira-watson.com/wp-content/uploads/2014/07/admobinterstitial_1-170x300.png)
![[Android] AdMob インタースティシャルの設置 admobinterstitial 2 170x300 - [Android] AdMob インタースティシャルの設置](https://akira-watson.com/wp-content/uploads/2014/07/admobinterstitial_2-170x300.png)
![[Android] AdMob インタースティシャルの設置 admobinterstitial 3 170x300 - [Android] AdMob インタースティシャルの設置](https://akira-watson.com/wp-content/uploads/2014/07/admobinterstitial_3-170x300.png)
この方法では同じActivity内での処理で終わっています。
ただ、Interstitial広告は画面遷移のタイミングで表示させたい場合が多いので、ここでは、それに追加、あるいは別のアプローチでの広告表示について考察してみました。
尚、バナー広告はこちらを参考に
Google Play Services SDK でAdMob広告を貼る
インタースティシャル広告の実装
実装の手順は、
- インタースティシャルを作成
- インタースティシャルをロードさせる
- ロードが終わったら表示
の順に作成します
gradleの設定です。
com.google.android.gms:play-services-ads:
を追加してください、またバージョンは適宜合わせこみをしないといけません。
build.gradle(Module: app)
1 2 3 4 5 6 7 8 9 10 |
apply plugin: 'com.android.application' android { ... } dependencies { ... implementation 'com.google.android.gms:play-services-ads:18.3.0' } |
Interstitial広告は強力であるため毎回表示しているとユーザーから嫌われます。表示頻度を数回に1回などのような調整が必要でしょう。
Unit ID(パブリッシャーID)とApp IDはそれぞれテスト用ですので、本番では自分の広告用IDを入れてください。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
package your.package.name; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.util.Log; import android.view.View; import android.widget.Button; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.InterstitialAd; import java.util.Random; public class MainActivity extends AppCompatActivity { // test private boolean EmulatorTest = true; private String UnitID; // publisher ID private String AdMobID = "interstitial IDを入れてください"; // Test ID private String EmulatorTestID = "ca-app-pub-3940256099942544/1033173712"; private InterstitialAd interstitialAd; // インタースティシャル用のView private View viewAd = null; // インタースティシャルを表示させる頻度の設定 // 毎回表示 private int Interval = 1; // private int Interval = 5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(EmulatorTest){ UnitID = EmulatorTestID; } else{ UnitID = AdMobID; } Log.d("debug", UnitID); // インタースティシャルを作成する。 interstitialAd = new InterstitialAd(getBaseContext()); interstitialAd.setAdUnitId(UnitID); // Set the AdListener. interstitialAd.setAdListener(new AdListener() { @Override public void onAdLoaded() { Log.d("debug", "onAdLoaded()"); } @Override public void onAdFailedToLoad(int errorCode) { String message = String.format("onAdFailedToLoad (%s)", getErrorReason(errorCode)); Log.d("debug","onAdFailedToLoad()"+message); } @Override public void onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. Log.d("debug","onAdOpened()"); } @Override public void onAdLeftApplication() { // Code to be executed when the user has left the app. Log.d("debug",""); } @Override public void onAdClosed() { // Code to be executed when when the user is about to return // to the application after tapping on an ad. Log.d("debug","onAdClosed()"); } }); // ページ移動間でインタースティシャルを表示 Button buttton = (Button)this.findViewById(R.id.button); buttton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplication(), SubActivity.class); startActivity(intent); showInterstitial(viewAd, Interval); } }); } // Called when the Load Interstitial button is clicked. public void loadInterstitial(View unusedView) { AdRequest adRequest; if(EmulatorTest){ // Test adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice(EmulatorTestID) .build(); } else{ // 広告リクエストを作成する (本番) adRequest = new AdRequest.Builder().build(); } // Load the interstitial ad. interstitialAd.loadAd(adRequest); } // Called when the Show Interstitial button is clicked. public void showInterstitial(View unusedView, int interval) { // ランダムに表示させる場合 if(interval !=0 ){ Random r = new Random(); int rand = r.nextInt(interval); Log.d("debug", "random="+rand); if(rand==0){ if (interstitialAd.isLoaded()) { interstitialAd.show(); } else { Log.d("debug", "Interstitial ad was not ready to be shown...... "); } } } } // Gets a string error reason from an error code. private String getErrorReason(int errorCode) { String errorReason = ""; switch(errorCode) { case AdRequest.ERROR_CODE_INTERNAL_ERROR: errorReason = "ERROR_CODE_INTERNAL_ERROR"; break; case AdRequest.ERROR_CODE_INVALID_REQUEST: errorReason = "ERROR_CODE_INVALID_REQUEST"; break; case AdRequest.ERROR_CODE_NETWORK_ERROR: errorReason = "ERROR_CODE_NETWORK_ERROR"; break; case AdRequest.ERROR_CODE_NO_FILL: errorReason = "ERROR_CODE_NO_FILL"; break; } return errorReason; } @Override protected void onStart() { super.onStart(); // load interstitial ad loadInterstitial(viewAd); Log.d("debug", "onStart()"); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:background="#cfd" tools:context=".MainActivity"> <TextView android:text="@string/main" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="50dp" android:text="@string/next" /> </LinearLayout> |
遷移先のActivityです
SubActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package your.package.name; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class SubActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); } } |
activity_sub.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:background="#cdf" tools:context=".SubActivity"> <TextView android:text="@string/sub" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> |
strings.xml
1 2 3 4 5 6 |
<resources> <string name="app_name">YourAppName</string> <string name="main">Main Activity</string> <string name="next">Next Page</string> <string name="sub">Sub Activity</string> </resources> |
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... <application ... android:theme="@style/AppTheme"> <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 --> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/> <activity android:name=".MainActivity"> ... </activity> <activity android:name=".SubActivity" android:label="SubActivity"> </activity> </application> </manifest> |
実行するとこんな感じです
ボタンをタップすると広告が表示されます
Xをタップして広告を消すと遷移先のActivity画面となる
良心的なアプリは5回に1回だけの頻度あたりで広告が出ますが、このあたりで手をうたないと、直ぐに削除されるでしょうね。それだけ、強力な広告です。
(捕捉)パブリッシャーID を取得
AdMobのフォームがよく変わるのですが基本的には同じです。
インタースティシャル広告用のパブリッシャーIDをゲットします
バナー用とは異なります
AdMob ヘルプセンター にてAdMob アカウントの開設をします。
アカウントを開設が終わってから、自分のアカウントに入ります。https://apps.admob.com/
「収益化」を選び、その後「新しいアプリの収益化」を選択
アプリ名とプラットフォームを入力
(表示画面は微妙に変化しますので注意してください)
既に公開されているなら「アプリを検索」
未公開であれば「アプリを手動で追加」のタブを選び
アプリ名とプラットフォームを入力
広告フォーマットの選択
ここでバナーとインタースティシャルのタブがありますので
インタースティシャルを選びます
広告ユニットIDが発行されます
最終的には、
ca-app-pub-XXXXXXXXXXXXXXXX/NNNNNNNNNN のような形式のIDが取得できます
関連ページ:
Reference:
https://developers.google.com/mobile-ads-sdk/docs/admob/advanced?hl=ja