facebook twitter hatena line email

Android/広告組込/google/admob/インタースティシャル

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

公式

admob基本 https://developers.google.com/admob/android/quick-start#update_your_androidmanifestxml

admobインステ https://developers.google.com/admob/android/interstitial

サンプル

AndroidManifest.xml

<manifest>
    <application>
        <!-- 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" />

build.gradle

buildscript {
    repositories {
        google()
    }
}

app/build.gradle

dependencies {
    implementation 'com.google.android.gms:play-services-ads:19.3.0'
}

MainActivity.java

package com.example.myadmobapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.RequestConfiguration;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private InterstitialAd mInterstitialAd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
                Log.d("TAG", "onInitializationComplete.");
            }
        });

        RequestConfiguration configuration =
                new RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("E36A38A1F26D812D88FB9711111111")).build(); // ここのtestDeviceIdは端末によって適宜違う
        MobileAds.setRequestConfiguration(configuration);

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());

        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Log.d("TAG", "The interstitial wasn't loaded yet.");
                }
            }
        });
    }
}

I/Ads: Ad failed to load : 0エラーが出る

targetSdkVersion 30(Android11β)でビルドすると、上記エラーが出る。 build.gradleのtargetSdkVersion 30から29にすると正常にインステ広告が出た。