Migrate to interstitial ad unit API for Android

Migrate to the LevelPlay Interstitial Ad Unit API by initializing the SDK with an app key and ad formats, creating a reusable interstitial ad object with a specific ad unit ID, and implementing a listener to handle ad events such as loading, displaying, rewarding, and closing.

Read time 6 minutes

This guide explains how to transition to the LevelPlay Interstitial APIs (using an ad unit ID) from your current implementation, to load and display interstitial ads.

Prerequisites

  • The min supported SDK is 8.6.0. You can download the latest SDK here.
  • Ensure that you initialize the SDK using LevelPlay Initialization API.
  • Find the AdUnitID in LevelPlay dashboard.

Create Interstitial Ad Object

The creation of the interstitial ad object must be performed after receiving the onInitSuccess callback.

The object is a reusable instance that can handle multiple loads and shows throughout the session. After creation, it should be used to load and show ads for the same ad unit.

For more advanced implementations, you may create multiple interstitial ad objects if necessary.

// Create interstitial ad object 
interstitialAd = new LevelPlayInterstitialAd(interstitialAdUnitId);

Register to Interstitial Events

Implement the LevelPlayInterstitialAdListener in your code instead of LevelPlayInterstitialListener to get informed of ad delivery. 

  • It is recommended to set the listener before loading the interstitial ad.
  • Each interstitial ad should have its own listener implementation.
  • Callbacks run on the main thread.
// Register to interstitial events
interstitialAd.OnAdLoaded += InterstitialOnAdLoadedEvent;
interstitialAd.OnAdLoadFailed += InterstitialOnAdLoadFailedEvent;
interstitialAd.OnAdDisplayed += InterstitialOnAdDisplayedEvent;
interstitialAd.OnAdDisplayFailed += InterstitialOnAdDisplayFailedEvent;
interstitialAd.OnAdClicked += InterstitialOnAdClickedEvent;
interstitialAd.OnAdClosed += InterstitialOnAdClosedEvent;
interstitialAd.OnAdInfoChanged += InterstitialOnAdInfoChangedEvent;

// Implement the events
void InterstitialOnAdLoadedEvent(LevelPlayAdInfo adInfo){}
void InterstitialOnAdLoadFailedEvent(LevelPlayAdError error){}
void InterstitialOnAdDisplayedEvent(LevelPlayAdInfo adInfo){}
void InterstitialOnAdDisplayFailedEvent(LevelPlayAdDisplayInfoError infoError){}
void InterstitialOnAdClickedEvent(LevelPlayAdInfo adInfo){}
void InterstitialOnAdClosedEvent(LevelPlayAdInfo adInfo){}
void InterstitialOnAdInfoChangedEvent(LevelPlayAdInfo adInfo){}
LegacyAd Unit (new)
ListenerLevelPlayInterstitialListenerLevelPlayInterstitialAdListener
CallbacksonAdReadyonAdLoaded 
onAdLoadFailedonAdLoadFailed
onAdOpenedonAdDisplayed
onAdClosedonAdClosed
onAdShowFailedonAdDisplayFailed
onAdClickedonAdClicked
onAdShowSucceeded- (deprecated)
onAdInfoChanged

LevelPlay Ad Info

Load Interstitial Ad

To load an interstitial ad, use loadAd instead of loadInterstitial.

// Load interstitial ad
interstitialAd.LoadAd();

Show Interstitial Ad

Show an interstitial ad after you receive the onAdLoaded callback using the LevelPlayInterstitialAdListener APIs.

  • If using placements, pass the placement name in the ShowAd API as shown in the Placements section below.
  • Once the ad has been successfully displayed to the user, you can load another ad by repeating the loading step.  

// Show ad without placement interstitialAd.ShowAd(); // Show ad with placement interstitialAd.ShowAd(placementName: "placementName");

Check Ad is Ready

To avoid show failures, and to make sure the ad could be displayed correctly, we recommend using the following API before calling the showAd API.

isAdReady – returns true if ad was loaded successfully and ad unit is not capped, or false otherwise.

isPlacementCapped – returns true when a valid placement is capped. If the placement is not valid, or not capped, this API will return false.

[code-snippet-android]

// Check that ad is ready and that the placement is not capped 
if (mInterstitialAd.isAdReady() && !LevelPlayInterstitialAd.isPlacementCapped(placementName)) 
{
    mInterstitialAd.showAd(this, placementName);
}

Placements

We support placements pacing and capping for interstitial on the LevelPlay dashboard. 

If placements are set up for interstitial ads, call the showAd method to serve the ad for a specific placement.

// Check that ad is ready and that the placement is not capped 
if(mInterstitialAd.isAdReady() && !LevelPlayInterstitialAd.isPlacementCapped(placementName)) 
{
    // Show ad with placement 
    mInterstitialAd.showAd(this, placementName);
}

Multiple Ad Unit Interstitial APIs

LegacyAd Unit (new)
APIloadInterstitialloadAd
showInterstitialshowAd
isInterstitialPlacementCappedLevelPlayInterstitialAd.isPlacementCapped
isInterstitialReadyisAdReady

Full Implementation Example of Interstitial Ads

public class InterstitialAdSample {
    private LevelPlayInterstitialAd interstitialAd
    void CreateInterstitialAd() {
	      //Create InterstitialAd instance
        interstitialAd= new LevelPlayInterstitialAd("interstitialAdUnitId");

        //Subscribe InterstitialAd events
        interstitialAd.OnAdLoaded += InterstitialOnAdLoadedEvent;
        interstitialAd.OnAdLoadFailed += InterstitialOnAdLoadFailedEvent;
        interstitialAd.OnAdDisplayed += InterstitialOnAdDisplayedEvent;
        interstitialAd.OnAdDisplayFailed += InterstitialOnAdDisplayFailedEvent;
        interstitialAd.OnAdClicked += InterstitialOnAdClickedEvent;
        interstitialAd.OnAdClosed += InterstitialOnAdClosedEvent;
        interstitialAd.OnAdInfoChanged += InterstitialOnAdInfoChangedEvent;
    }
    void LoadInterstitialAd() {
        //Load or reload InterstitialAd 	
        interstitialAd.LoadAd();
    }
    void ShowInterstitialAd() {
	       //Show InterstitialAd, check if the ad is ready before showing
        if (interstitialAd.IsAdReady()) {
   		      interstitialAd.ShowAd();
        }
    }
    
    void DestroyInterstitialAd() {
	      //Destroy InterstitialAd 
        interstitialAd.DestroyAd();
    }
  
    //Implement InterstitialAd events
    void InterstitialOnAdLoadedEvent(LevelPlayAdInfo adInfo) { }
    void InterstitialOnAdLoadFailedEvent(LevelPlayAdError ironSourceError) { }
    void InterstitialOnAdClickedEvent(LevelPlayAdInfo adInfo) { }
    void InterstitialOnAdDisplayedEvent(LevelPlayAdInfo adInfo) { }
    void InterstitialOnAdDisplayFailedEvent(LevelPlayAdDisplayInfoError adInfoError) { }
    void InterstitialOnAdClosedEvent(LevelPlayAdInfo adInfo) { }
    void InterstitialOnAdInfoChangedEvent(LevelPlayAdInfo adInfo) { }
}

Done!

You are now all set up to serve interstitial ads in your application using our new Multiple Ad Unit APIs.