目录

Splash ads are displayed immediately after an app is launched, even before the home screen of the app is displayed.

1. Adding a Splash Ad

Step 1: Adding SplashView

Add SplashView to an XML layout file.

The following sample code shows how to add SplashView to an XML layout file:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    xmlns:tools="http://schemas.android.com/tools"
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent"
  6.    tools:context=".SplashActivity">
  7.    <RelativeLayout
  8.        android:id="@+id/logo"
  9.        android:layout_width="fill_parent"
  10.        android:layout_height="100dp"
  11.        android:layout_alignParentBottom="true"
  12.        android:visibility="visible">
  13.        ...
  14.    </RelativeLayout>
  15.    <com.huawei.hms.ads.splash.SplashView
  16.        android:id="@+id/splash_ad_view"
  17.        android:layout_width="match_parent"
  18.        android:layout_height="match_parent"
  19.        android:layout_above="@id/logo" />
  20. </RelativeLayout>

The following sample code shows how to obtain SplashView:

  1. SplashView splashView = findViewById(R.id.splash_ad_view);

Step 2: Modifying the Default Launch Screen of Your App

To display a splash ad prior to your app's home screen, you need to modify the default launch screen of the app.

Modify the AndroidManifest.xml file by changing the activity started by default to Splash Activity. In this way, a splash ad can be displayed before your app's home screen is loaded. An example of the modified file content is as follows:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="com.huawei.hms.ads.sdk">
  4.    <application
  5.        android:allowBackup="true"
  6.        android:icon="@mipmap/ic_launcher"
  7.        android:label="@string/app_name"
  8.        android:roundIcon="@mipmap/ic_launcher_round"
  9.        android:supportsRtl="true"
  10.        android:theme="@style/AppTheme">
  11.        <activity
  12.            android:name=".MainActivity"
  13.            android:exported="false"
  14.            android:screenOrientation="portrait">
  15.        </activity>
  16.        <activity
  17.            android:name=".SplashActivity"
  18.            android:exported="false"
  19.            android:screenOrientation="portrait">
  20.            <intent-filter>
  21.                <action android:name="android.intent.action.MAIN" />
  22.                <category android:name="android.intent.category.LAUNCHER" />
  23.            </intent-filter>
  24.        </activity>
  25.        ...
  26.    </application>
  27. </manifest>

Create a SplashActivity.java class for loading and displaying a splash ad.

  1. ...
  2. import android.os.Build;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. public class SplashActivity extends AppCompatActivity {
  5.    private static final String AD_ID = "testq6zq98hecj";
  6.    /**
  7.     * Pause flag.
  8.     * On the splash ad screen:
  9.     * Set this parameter to true when exiting the app to ensure that the app home screen is not displayed.
  10.     * Set this parameter to false when returning to the splash ad screen from another screen to ensure that the app home screen can be displayed properly.
  11.     */
  12.    private boolean hasPaused = false;
  13.    @Override
  14.    protected void onCreate(Bundle savedInstanceState) {
  15.        super.onCreate(savedInstanceState);
  16.        setContentView(R.layout.activity_splash);
  17.        // Load and display a splash ad.
  18.        loadAd();
  19.    }
  20.    /**
  21.     * When the ad display is complete, the app home screen is displayed.
  22.     */
  23.    private void jump() {
  24.        if (!hasPaused) {
  25.            hasPaused = true;
  26.            startActivity(new Intent(SplashActivity.this, MainActivity.class));
  27.            finish();
  28.        }
  29.    }
  30.    /**
  31.     * Set this parameter to true when exiting the app to ensure that the app home screen is not displayed.
  32.     */
  33.    @Override
  34.    protected void onStop() {
  35.        // Remove the timeout message from the message queue.
  36.        timeoutHandler.removeMessages(MSG_AD_TIMEOUT);
  37.        hasPaused = true;
  38.        super.onStop();
  39.    }
  40.    /**
  41.     * Call this method when returning to the splash ad screen from another screen to access the app home screen.
  42.     */
  43.    @Override
  44.    protected void onRestart() {
  45.        super.onRestart();
  46.        hasPaused = false;
  47.        jump();
  48.    }
  49.    @Override
  50.    protected void onDestroy() {
  51.        super.onDestroy();
  52.    }
  53. }

Step 3: Loading an Ad

After adding SplashView, load an ad using the load() method in the SplashView class. The sample code is as follows:

  1. private void loadAd() {
  2.    int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
  3.    AdParam adParam = new AdParam.Builder().build();
  4.    SplashView.SplashAdLoadListener splashAdLoadListener = new SplashView.SplashAdLoadListener() {
  5.        @Override
  6.        public void onAdLoaded() {
  7.            // Called when an ad is loaded successfully.
  8.        }
  9.        @Override
  10.        public void onAdFailedToLoad(int errorCode) {
  11.            // Called when an ad failed to be loaded. The app home screen is then displayed.
  12.            jump();
  13.        }
  14.        @Override
  15.        public void onAdDismissed() {
  16.            // Called when the display of an ad is complete. The app home screen is then displayed.
  17.            jump();
  18.        }
  19.    };
  20.    // Obtain SplashView.
  21.    SplashView splashView = findViewById(R.id.splash_ad_view);
  22.    // Set the audio focus preemption policy for a video splash ad.
  23.    splashView.setAudioFocusType(AudioFocusType.NOT_GAIN_AUDIO_FOCUS_WHEN_MUTE);
  24.    // Load the ad. AD_ID indicates the ad slot ID.
  25.    splashView.load(AD_ID, orientation, adParam, splashAdLoadListener);
  26. }

(Optional) Step 4: Listening for Ad Events

You can listen for ad events using the methods in the SplashAdDisplayListener class.

  1. SplashAdDisplayListener adDisplayListener = new SplashAdDisplayListener() {
  2.    @Override
  3.    public void onAdShowed() {
  4.        // Called when an ad is displayed.
  5.    }
  6.    @Override
  7.    public void onAdClick() {
  8.        // Called when an ad is clicked.
  9.    }
  10. };
  11. splashView.setAdDisplayListener(adDisplayListener);

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

2. Testing a Splash Ad

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

The following table lists the dedicated ad slot IDs for splash ad testing.

Ad Format

Display Form

Resolution

Aspect Ratio

Test Ad Slot ID

Splash

Image

1080*1620

2:3

testq6zq98hecj

Splash

Video

720*1280

9:16

testd7c5cewoj6

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