kamranbekirovyz
kamranbekirovyz

Reputation: 1321

How to check avaliability of ads in AdMob?

I want to show my users interstitial and rewarded ads of AdMob in exchange for some coins for the game. But, before poping up screen for that I want to check if there are avaliable ads for that?

Any knows how to do that ? I would really appreciate!

Upvotes: 0

Views: 58

Answers (2)

Zia
Zia

Reputation: 755

you can check AdMob interstitial ads like this. 1. Suppose you have an interstitial like this.

    InterstitialAd ad;
ad = new InterstitialAd(this);
        ad.setAdUnitId(getString(R.string.interstitial));
        ad.loadAd(new AdRequest.Builder().build());

        ad.setAdListener(new AdListener(){
            @Override
            public void onAdClosed() {
                super.onAdClosed();
                startActivity(new Intent(getApplicationContext(), SomeActivity.class));
                finish();
            }
        });

Now, use this building function to check the availability of interstitial ads.

if(ad.isLoaded()) {
  // This will only exicute when ad is avalibe to display
  ad.show();
}
else {
  Toast.makeText(this, "Ad not available", Toast.LENGTH_SHORT).show();
}

Upvotes: 1

S. Gissel
S. Gissel

Reputation: 2650

Before showing an ad #onRewardedAdLoaded is called. You can check if an ad is available with this function.

public class MainActivity extends Activity {

    private RewardedAd rewardedAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        rewardedAd = new RewardedAd(this,
        "ca-app-pub-3940256099942544/5224354917");

        RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
            @Override
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.
            }

           @Override
            public void onRewardedAdFailedToLoad(int errorCode) {
                // Ad failed to load.
            }
        };
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
    }
}

You should call #isLoaded before showing the rewardedAd anyway.

if (rewardedAd.isLoaded()) {
    rewardedAd.show(activityContext, adCallback); 
}

Upvotes: 1

Related Questions