Question

I used to display AdMob banner on my future apps, and I'd like to give a try to the interstitial ads.

I checked the AdMob SDK for implementation, and I copied their example source because it was exactly what I want (i.e. the interstitial shown when the activity launch).

I tried it on emulator and on my Galaxy, no ad has been displayed.

Here is the source code:

public class Asscreed extends Activity {
    private InterstitialAd interstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asscreed);

        // Create the interstitial.
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-6046034785851961/xxxxxx");

        // Create ad request.
        AdRequest adRequest = new AdRequest.Builder().build();

        // Begin loading your interstitial.
        interstitial.loadAd(adRequest);
    }

    // Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
}

The imports are OK and the Google Play Services library is of course imported.

I use this example: AdMob Android Guides - Interstitial Ad.

Could someone tell me what's wrong in my code?

Était-ce utile?

La solution

You should wait for the ad to be loaded. Only then, you can call displayInterstial() method, which would show the ad.

You can register for a listener, that will let you know when the loading is done.

interstitial.setAdListener(new AdListener(){
          public void onAdLoaded(){
               displayInterstitial();
          }
});

Autres conseils

this did it for me.

// Begin listening to interstitial & show ads.
interstitial.setAdListener(new AdListener(){
     public void onAdLoaded(){
          interstitial.show();
     }
});

i still wonder why all the guys at google upload code implementing directions that just don't work after one follows them to the dot..

I had the same problem. The issue was that admob activity was not defined in manifest. Please make sure you have folllowing activity tag in your manifest file

<activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />
mInterstitialAd = new InterstitialAd(this);
                mInterstitialAd.setAdUnitId("Your Interstitial Id ca-app-pub-46563563567356235/3452455");
                AdRequest adRequest1 = new AdRequest.Builder()
                        .build();
                mInterstitialAd.loadAd(adRequest1);
                mInterstitialAd.setAdListener(new com.google.android.gms.ads.AdListener() {
                    @Override
                    public void onAdLoaded() {
                        mInterstitialAd.show();
                        super.onAdLoaded();

                    }
                });

Try initializing it in onCreate, but only call the show() method from an event, such as a button click.

You can use this code, the test ids work:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    ...

        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); //test id

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //test ad

        mInterstitialAd.loadAd(new AdRequest.Builder().build());


        btn_test.setOnClickListener(view -> {
            showInterstitial();
        });
...
    }    
    private void showInterstitial()
    {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
    }

in your AndroidManifest you must put the tags

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:theme="@android:style/Theme.Translucent"
        tools:replace="android:theme" />
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="APP_ID" />
    <meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Ad Manager

Ad Mob

You didn't call displayInsterstitial().

But what you should do is call the listener for the interstitial :

interstitial.setAdListener(this);

It will then implements the Listener to your Activity and then display it onAdLoaded (or something).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top