We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.

HMS Core GuidesAds KitPublisher ServiceApp DevelopmentAd FormatsInterstitial Ads

Interstitial Ads

Interstitial ads are full-screen ads that cover the interface of an app. Such ads are displayed when a user starts, pauses, or exits an app, without disrupting the user's experience.

           

1. Adding an Interstitial Ad

Step 1: Creating an Interstitial Ad Object

Create an InterstitialAd object and use the setAdId() method of the InterstitialAd class to set a test ad slot ID. The sample code is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. private void loadInterstitialAd() {
  2.    interstitialAd = new InterstitialAd(this);
  3.    interstitialAd.setAdId("testb4znbuh3n2"); // testb4znbuh3n2 is a dedicated test ad slot ID.
  4.    ...
  5. }

Step 2: Loading an Ad

Call the loadAd() method of the InterstitialAd object to load an ad. The sample code is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. private void loadInterstitial() {
  2.    AdParam adParam = new AdParam.Builder().build();
  3.    interstitialAd.loadAd(adParam);
  4.    ...
  5. }

Step 3: Displaying an Ad

Call the isLoaded() method to confirm that an ad has finished loading, and call the show() method of the InterstitialAd object to display the ad.

Collapse
Word wrap
Dark theme
Copy code
  1. private void showInterstitial() {
  2.    if (interstitialAd != null && interstitialAd.isLoaded()) {
  3.        interstitialAd.show();
  4.    } else {
  5.        Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
  6.    }
  7. }

 Step 4: Listening for Ad Events

Call the setAdListener method of the InterstitialAd class to add the ad event listener AdListener for the InterstitialAd object, and implement the methods in AdListener to listen to ad events.

Collapse
Word wrap
Dark theme
Copy code
  1. ...
  2. interstitialAd.setAdListener(adListener);
  3. ...
  4. private AdListener adListener = new AdListener() {
  5.    @Override
  6.    public void onAdLoaded() {
  7.        // Called when an ad is loaded successfully.
  8.        ...
  9.        showInterstitialAd();
  10.    }
  11.    @Override
  12.    public void onAdFailed(int errorCode) {
  13.        // Called when an ad fails to be loaded.
  14.    }
  15.    @Override
  16.    public void onAdClosed() {
  17.        // Called when an ad is closed.
  18.    }
  19.    @Override
  20.    public void onAdClicked() {
  21.        // Called when an ad is clicked.
  22.    }
  23.    @Override
  24.    public void onAdLeave() {
  25.        // Called when a user leaves an ad.
  26.    }
  27.    @Override
  28.    public void onAdOpened() {
  29.        // Called when an ad is opened.
  30.    }
  31.    @Override
  32.    public void onAdImpression() {
  33.        // Called when an ad impression occurs.
  34.    }
  35. };
  36. ...

For details about the methods in AdListener, please refer to the  API Reference.

2. Testing an Interstitial Ad

When testing interstitial ads, use the dedicated test ad slot ID to obtain test ads. This avoids invalid ad clicks during the test. The test ad slot ID is used only for function commissioning, but not for ad monetization. Before releasing your app, apply for a formal ad slot ID and replace the test ad slot ID with the formal one.

The following table lists the dedicated ad slot ID for interstitial ad testing.

Expand

Ad Format

Display Form

Screen Orientation

Size

Test Ad Slot ID

Interstitial

Image

Portrait

1080 x 1620

teste9ih9j0rc3

Interstitial

Video

Portrait

720 x 1080

testb4znbuh3n2

You can download the sample code for interstitial ads and execute the code. The following screen will be displayed.

 



This page may contain third-party content. For details, click here.