Új hirdetés API használata egyszerűen

Roguelite Dungeon Crawler · Blue Experiment

package io.itch.blue_experiment.card1;

import io.itch.blue_experiment.card1.BookletGame;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.LoadAdError;

public class AndroidAdvertisement implements BookletGame.Advertisement {
  private AndroidApplication application;
  private InterstitialAd interstitialAd;
  private AdRequest.Builder requestBuilder;

  @Override public void showInterstitial() {
    synchronized(application) {
      application.runOnUiThread(new Runnable() {
        @Override public void run() {
          if (interstitialAd != null) interstitialAd.show(application);
          synchronized(application) {application.notify();}
          loadNextAd();
        }
      });
      try {application.wait();} catch (InterruptedException exception) { }
    }
  }

  public AndroidAdvertisement(AndroidApplication application) {
    MobileAds.initialize(this.application = application, new OnInitializationCompleteListener() {
      @Override public void onInitializationComplete(InitializationStatus status) {loadNextAd();}
    });
  }

  private void loadNextAd() {
    InterstitialAd.load(application, "ca-app-pub-3940256099942544/1033173712",
      new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {
        @Override public void onAdLoaded(InterstitialAd ad) {interstitialAd = ad;}
        @Override public void onAdFailedToLoad(LoadAdError error) {interstitialAd = null;}
    });
  }
}