Search code examples
javaandroidadmobinterstitial

Why Is Admob Interstitial Ad Not Showing On AndroidOS?


I apply the ad codes exactly as Google published, but the ad is not shown.

My codes:

AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load( this, "ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() { //test reklam id
    @Override
    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
        // The mInterstitialAd reference will be null until
        // an ad is loaded.
        mInterstitialAd = interstitialAd;
        Log.i( TAG, "onAdLoaded" );
    }

    @Override
    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Handle the error
        Log.i( TAG, loadAdError.getMessage() );
        mInterstitialAd = null;
    }
});

Here is the button code I use to show the ad:

btnGo.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mInterstitialAd != null) {
            mInterstitialAd.show(MainActivity.this);
            Log.e( "ads", "must be shown" );
        }
        startActivity( new Intent( MainActivity.this, SecondActivity.class ) );
    }
});

When the ad is loaded, I get a loaded log about it, it seems like there is no problem. I check if the ad is null for showing ad status, and I get an ad showing if it is not null, which is fine. The only problem is that the ad isn't showing.

What could be the problem or what am I doing wrong?


Solution

  • Problem solved:

    if (mInterstitialAd != null) {
        mInterstitialAd.show(MainActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
          @Override
          public void onAdDismissedFullScreenContent() {
            // Called when fullscreen content is dismissed.
        startActivity( new Intent( MainActivity.this, SecondActivity.class ) );
            Log.d("TAG", "The ad was dismissed.");
          }
    
          @Override
          public void onAdFailedToShowFullScreenContent(AdError adError) {
            // Called when fullscreen content failed to show.
        startActivity( new Intent( MainActivity.this, SecondActivity.class ) );
            Log.d("TAG", "The ad failed to show.");
          }
    
          @Override
          public void onAdShowedFullScreenContent() {
            // Called when fullscreen content is shown.
            // Make sure to set your reference to null so you don't
            // show it a second time.
            mInterstitialAd = null;
            Log.d("TAG", "The ad was shown.");
          }
        });
    } else {
        startActivity( new Intent( MainActivity.this, SecondActivity.class ) );
                    }