Overview

The express splash ads service is a new traffic monetization solution provided by HUAWEI Ads to improve splash ad experience without requiring you to integrate any SDK.
This codelab will step you through the process of enabling the express splash ads service and correctly displaying an ad in your app.

What You Will Create

In this codelab, you will build an empty app and enable the express splash ads service. Your app will:

What You Will Learn

In this codelab, you will learn how to:

Software Requirements

Check method:
To view the HMS Core (APK) version on a device, go to Settings > Apps > Apps and search for HMS Core.
To view the EMUI version on a device, go to Settings > System > About phone.

Creating a Project in Android Studio

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

Create an AIDL file for the ExSplashService API and save the file under the com.huawei.hms.ads package, as shown in the following figure.

Copy the following content to the AIDL file:

package com.huawei.hms.ads; interface ExSplashService { void enableUserInfo(boolean enable); }

Create a class that implements Android-native ServiceConnection.

  1. Implement the onServiceConnected method of ServiceConnection.
  2. Call the Android-native ExSplashService.Stub.asInterface method to obtain ExSplashService.
  3. Call the enableUserInfo method to synchronize the user agreement status.
    public final class ExSplashServiceConnection implements ServiceConnection { private static final String TAG = "ExSplashServiceConnection"; private Context context; public ExSplashServiceConnection(Context context) { this.context = context; } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, "onServiceConnected"); ExSplashService exSplashService = ExSplashService.Stub.asInterface(service); if (exSplashService != null) { try { // true: The user agrees to give consent. false: The user does not agree to give consent. exSplashService.enableUserInfo(true); } catch (RemoteException e) { Log.i(TAG, "enableUserInfo error"); } finally { context.unbindService(this); } } } @Override public void onServiceDisconnected(ComponentName name) { Log.i(TAG, "onServiceDisconnected"); } }

Connect to the AIDL service of express splash ads.

  1. Create an ExSplashServiceConnection instance.
  2. Create the Intent object with the com.huawei.hms.ads.EXSPLASH_SERVICE action.
  3. Set the package name of Intent to com.huawei.hwid.
  4. Call bindService to connect to the AIDL service.
    private boolean bindService(Context context) { ExSplashServiceConnection serviceConnection = new ExSplashServiceConnection(context); Intent intent = new Intent("com.huawei.hms.ads.EXSPLASH_SERVICE"); intent.setPackage("com.huawei.hwid"); boolean result = context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); Log.i(TAG, "bindService result: " + result); return result; }

During express splash ad display, HUAWEI Ads sends a broadcast message to notify your app. The broadcast action is com.huawei.hms.ads.EXSPLASH_DISPLAYED. Your app can register a broadcast receiver to receive such messages and determine what to do after receiving the messages.

Create a broadcast receiver that inherits from BroadcastReceiver, and override the onReceive method.

public class ExSplashBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent == null) { return; } String action = intent.getAction(); if (action.equals("com.huawei.hms.ads.EXSPLASH_DISPLAYED")) { // Do your stuff here } } }

Register the broadcast receiver during app launch (recommended time).

IntentFilter filter = new IntentFilter("com.huawei.hms.ads.EXSPLASH_DISPLAYED"); registerReceiver(new ExSplashBroadcastReceiver(), filter);

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

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

For more information, please click the following links:

Download the demo source code used in this codelab from the following address:

Download source code

Code copied