Migrate to interstitial ad unit API for Android
Migrate to the LevelPlay Interstitial Ad Unit API by initializing the SDK with an app key, creating a reusable interstitial ad object with a specific ad unit ID, and implementing a listener to handle ad events such as loading, displaying, and closing.
読み終わるまでの所要時間 3 分最終更新 3日前
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 admInterstitialAd = new LevelPlayInterstitialAd("adUnitId");
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.
// Create interstitial admInterstitialAd = new LevelPlayInterstitialAd("adUnitId");// Set listenermInterstitialAd.setListener(this);// LevelPlayInterstitialAdListener methods@Overridepublic void onAdLoaded(@NonNull LevelPlayAdInfo adInfo) {}@Overridepublic void onAdLoadFailed(@NonNull LevelPlayAdError error) {}@Overridepublic void onAdDisplayed(@NonNull LevelPlayAdInfo adInfo) {}@Overridepublic void onAdDisplayFailed(@NonNull LevelPlayAdError error, @NonNull LevelPlayAdInfo adInfo) {}@Overridepublic void onAdClosed(@NonNull LevelPlayAdInfo adInfo) {}@Overridepublic void onAdClicked(@NonNull LevelPlayAdInfo adInfo) {}@Overridepublic void onAdInfoChanged(@NonNull LevelPlayAdInfo adInfo) {}
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
Load Interstitial Ad
To load an interstitial ad, use loadAd instead of loadInterstitial.// Load or reload the admInterstitialAd.loadAd();
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.
// Show ad without placementmInterstitialAd.showAd(this);// Show ad with placementmInterstitialAd.showAd(this, 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.// Check that ad is ready and that the placement is not cappedif (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 cappedif (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() { mInterstitialAd.loadAd(); } void showInterstitialAd() { if (mInterstitialAd.isAdReady()) { mInterstitialAd.showAd(this); } } void showInterstitialAd(@NonNull String placementName) { 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) {}}