Overview

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.
This codelab will step you through the process of adding an interstitial ad and correctly displaying it in your app.

What You Will Create

In this codelab, you will build an empty app and integrate an interstitial ad into it.
Your app will:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

To view the HMS Core (APK) version on a device, go to Settings > Apps > Apps and search for HMS Core.

Creating a Project in Android Studio

  1. Start Android Studio.
  2. Start a new project, select Empty Activity, and name the project InterstitialExample.
  3. Click Finish.

Configuring the HUAWEI Ads SDK Repository and Dependency Package

Configure the Maven repository address in the project-level build.gradle file.

buildscript { repositories { google() jcenter() maven { url 'https://developer.huawei.com/repo/' } } ... } allprojects { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } }

Configure the dependency package in the app-level build.gradle file, and replace {version} with the latest version of HUAWEI Ads SDK. For details, please refer to Version Change History.

dependencies { ... implementation 'com.huawei.hms:ads-lite:{version}' ... }

Click Sync Now and wait until synchronization is complete.

Configuring Obfuscation Scripts

Before building the APK, configure obfuscation scripts to prevent the HUAWEI Ads SDK from being obfuscated. If obfuscation arises, the HUAWEI Ads SDK may not function properly.

  1. Open the obfuscation script file of your Android project.
  2. Add the following two lines of code to the app/proguard-rules.pro file to exclude the HUAWEI Ads SDK from obfuscation:
    -keep class com.huawei.openalliance.ad.** { *; } -keep class com.huawei.hms.ads.** { *; }

To obtain ads using the HUAWEI Ads SDK, your app must initialize the SDK when starting up.
Call HwAds.init() in the onCreate() method of the MainActivity class to initialize the HUAWEI Ads SDK.
MainActivity.java

... import com.huawei.hms.ads.HwAds; ... public class MainActivity extends AppCompatActivity { ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the HUAWEI Ads SDK. HwAds.init(this); ... } ... }

The HUAWEI Ads SDK provides the InterstitialAd class for creating an interstitial ad object, and uses the setAdId() method of the InterstitialAd class to set the test ad slot ID.
The sample code is as follows:
MainActivity.java

private void loadInterstitialAd() { interstitialAd = new InterstitialAd(this); interstitialAd.setAdId(getAdId()); // Set an ad slot ID. ... } private String getAdId() { if (displayRadioGroup.getCheckedRadioButtonId() == R.id.display_image) { return getString(R.string.image_ad_id); // The value of image_ad_id is teste9ih9j0rc3. } else { return getString(R.string.video_ad_id); // The value of video_ad_id is testb4znbuh3n2. } }

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

private void loadInterstitialAd() { interstitialAd = new InterstitialAd(this); interstitialAd.setAdId(getAdId()); // Set an ad slot ID. interstitialAd.setAdListener(adListener); // Load an interstitial ad. AdParam adParam = new AdParam.Builder().build(); interstitialAd.loadAd(adParam); ... }

Call the setAdListener method of the InterstitialAd class to add AdListener for the InterstitialAd object.
The sample code is as follows:
MainActivity.java

private AdListener adListener = new AdListener() { @Override public void onAdLoaded() { super.onAdLoaded(); Toast.makeText(MainActivity.this, "Ad loaded", Toast.LENGTH_SHORT).show(); // Display an interstitial ad. showInterstitial(); } @Override public void onAdFailed(int errorCode) { Toast.makeText(MainActivity.this, "Ad load failed with error code: " + errorCode, Toast.LENGTH_SHORT).show(); Log.d(TAG, "Ad load failed with error code: " + errorCode); } @Override public void onAdClosed() { super.onAdClosed(); Log.d(TAG, "onAdClosed"); } @Override public void onAdClicked() { Log.d(TAG, "onAdClicked"); super.onAdClicked(); } @Override public void onAdOpened() { Log.d(TAG, "onAdOpened"); super.onAdOpened(); } };

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

The sample code is as follows:
MainActivity.java

private void showInterstitial() { // Display an interstitial ad. if (interstitialAd != null && interstitialAd.isLoaded()) { interstitialAd.show(); } else { Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show(); } }

After running the project, you will see the interstitial ad in your app.

Well done. You have successfully completed this codelab and learned how to:

For more information, please click the following links:

You can click the button below to download the source code.

Download source code

Code copied