Migrate to Interstitial Ad Unit API (Beta)
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.
Create Interstitial Ad Object
The creation of the interstitial ad object should be done after receiving onInitSuccess callback.
LevelPlayInterstitialAd mInterstitialAd = new LevelPlayInterstitialAd("adUnitId");
val mInterstitialAd = LevelPlayInterstitialAd("adUnitId")
Set Interstitial Listener
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.
- Please note that each interstitial ad should have its own listener implementation.
- Callbacks run on the main thread.
// Create the interstitial ad
LevelPlayInterstitialAd mInterstitialAd = new LevelPlayInterstitialAd("adUnitId");
mInterstitialAd.setListener(new LevelPlayInterstitialAdListener() {
@Override
public void onAdLoaded(LevelPlayAdInfo levelPlayAdInfo) {
// Ad was loaded successfully
}
@Override
public void onAdLoadFailed(LevelPlayAdError levelPlayAdError) {
// Ad load failed
}
@Override
public void onAdDisplayed(LevelPlayAdInfo levelPlayAdInfo) {
// Ad was displayed and visible on screen
}
@Override
public void onAdDisplayFailed(LevelPlayAdError levelPlayAdError, LevelPlayAdInfo levelPlayAdInfo) {
// Ad fails to be displayed
// Optional
}
@Override
public void onAdClicked(LevelPlayAdInfo levelPlayAdInfo) {
// Ad was clicked
// Optional
}
@Override
public void onAdClosed(LevelPlayAdInfo levelPlayAdInfo) {
// Ad was closed
// Optional
}
@Override
public void onAdInfoChanged(LevelPlayAdInfo levelPlayAdInfo) {
// Called after the ad info is updated. Available when another interstitial ad has loaded, and includes a higher CPM/Rate
// Optional
}
});
// Create the interstitial ad unit
val mInterstitialAd = LevelPlayInterstitialAd("adUnitId")
mInterstitialAd.setListener(object : LevelPlayInterstitialAdListener {
override fun onAdLoaded(levelPlayAdInfo: LevelPlayAdInfo) {
// Ad was loaded successfully
}
override fun onAdLoadFailed(levelPlayAdError: LevelPlayAdError) {
// Ad load failed
}
override fun onAdDisplayed(levelPlayAdInfo: LevelPlayAdInfo) {
// Ad was displayed and visible on screen
}
override fun onAdDisplayFailed(levelPlayAdError: LevelPlayAdError, levelPlayAdInfo: LevelPlayAdInfo) {
// Ad fails to be displayed
// Optional
}
override fun onAdClicked(levelPlayAdInfo: LevelPlayAdInfo) {
// Ad was clicked
// Optional
}
override fun onAdClosed(levelPlayAdInfo: LevelPlayAdInfo) {
// Ad was closed
// Optional
}
override fun onAdInfoChanged(levelPlayAdInfo: LevelPlayAdInfo) {
// Called after the ad info is updated. Available when another interstitial ad has loaded, and includes a higher CPM/Rate
// Optional
}
})
Legacy | Ad Unit (new) | |
Listener | LevelPlayInterstitialListener | LevelPlayInterstitialAdListener |
Callbacks | onAdReady | onAdLoaded |
onAdLoadFailed | onAdLoadFailed | |
onAdOpened | onAdDisplayed | |
onAdClosed | onAdClosed | |
onAdShowFailed | onAdDisplayFailed | |
onAdClicked | onAdClicked | |
onAdShowSucceeded | – (deprecated) | |
onAdInfoChanged |
LevelPlay Ad Info
The AdInfo class returned by the interstitial listener callbacks has been replaced by LevelPlayAdInfo.
Learn more about its implementation and available fields here.
Load Interstitial Ad
To load an interstitial ad, use loadAd instead of loadInterstitial.
Show Interstitial Ad
Show an interstitial ad after you receive the onAdLoaded callback using the LevelPlayInterstitialAdListener APIs.
- It is required to share Activity.
- 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.
public void showInterstitialAd() {
// Check that ad is ready
if (mInterstitialAd.isAdReady()) {
// Show ad
mInterstitialAd.showAd(this);
}
}
fun showInterstitialAd() {
// Check that ad is ready
if (mInterstitialAd.isAdReady()) {
// Show ad
mInterstitialAd.showAd(this)
}
}
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.
// Check that ad is ready and that the placement is not capped
if (mInterstitialAd.isAdReady() && !LevelPlayInterstitialAd.isPlacementCapped(placementName)) {
mInterstitialAd.showAd(this, placementName);
}
// 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);
}
// 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
Legacy | Ad Unit (new) | |
API | loadInterstitial | loadAd |
showInterstitial | showAd | |
isInterstitialPlacementCapped | LevelPlayInterstitialAd.isPlacementCapped | |
isInterstitialReady | isAdReady |
Full Implementation Example of Interstitial Ads
public class InterstitialAdActivity extends Activity implements LevelPlayInterstitialAdListener {
private LevelPlayInterstitialAd mInterstitialAd;
void createInterstitialAd() {
mInterstitialAd = new LevelPlayInterstitialAd("adUnitId");
mInterstitialAd.setListener(this);
}
void loadInterstitialAd() {
// Used to load or reload the ad
mInterstitialAd.loadAd();
}
void showInterstitialAd() {
if (mInterstitialAd.isAdReady()) {
mInterstitialAd.showAd(this);
}
}
void showInterstitialAd(@NonNull String placementName) {
// Check that ad is ready and that the placement is not capped
if (mInterstitialAd.isAdReady() && !LevelPlayInterstitialAd.isPlacementCapped(placementName)) {
mInterstitialAd.showAd(this, placementName);
}
}
// LevelPlayInterstitialAdListener methods
@Override
public void onAdLoaded(@NonNull LevelPlayAdInfo adInfo) {}
@Override
public void onAdLoadFailed(@NonNull LevelPlayAdError error) {}
@Override
public void onAdDisplayed(@NonNull LevelPlayAdInfo adInfo) {}
@Override
public void onAdClosed(@NonNull LevelPlayAdInfo adInfo) {}
@Override
public void onAdClicked(@NonNull LevelPlayAdInfo adInfo) {}
@Override
public void onAdDisplayFailed(@NonNull LevelPlayAdError error, @NonNull LevelPlayAdInfo adInfo) {}
@Override
public void onAdInfoChanged(@NonNull LevelPlayAdInfo adInfo) {}
}
class InterstitialAdActivity : Activity(), LevelPlayInterstitialAdListener {
private lateinit var mInterstitialAd: LevelPlayInterstitialAd
fun createInterstitialAd() {
mInterstitialAd = LevelPlayInterstitialAd("adUnitId")
mInterstitialAd.setListener(this)
}
fun loadInterstitialAd() {
mInterstitialAd.loadAd()
}
fun showInterstitialAd() {
if (mInterstitialAd.isAdReady()) {
mInterstitialAd.showAd(this)
}
}
fun showInterstitialAd(placementName: String) {
// Check that ad is ready and that the placement is not capped
if (mInterstitialAd.isAdReady() && !LevelPlayInterstitialAd.isPlacementCapped(placementName)) {
mInterstitialAd.showAd(this, placementName)
}
}
// LevelPlayInterstitialAdListener methods
override fun onAdLoaded(adInfo: LevelPlayAdInfo) {}
override fun onAdLoadFailed(error: LevelPlayAdError) {}
override fun onAdInfoChanged(adInfo: LevelPlayAdInfo) {}
override fun onAdDisplayed(adInfo: LevelPlayAdInfo) {}
override fun onAdDisplayFailed(error: LevelPlayAdError, adInfo: LevelPlayAdInfo) {}
override fun onAdClicked(adInfo: LevelPlayAdInfo) {}
override fun onAdClosed(adInfo: LevelPlayAdInfo) {}
}
LevelPlay Mediation Demo App
The Integration Demo application demonstrates how to integrate interstitial Ad Unit APIs in your app.
Download Android Demo Application
Done!
You can now deliver interstitial ads in your app using our new Multiple Ad Unit APIs.