Beginner: Express Splash Ads Integration using Huawei Ads Kit

Anand Maalige
Huawei Developers
Published in
5 min readApr 4, 2023
Ads kit

Introduction

Huawei Ads Kit provides high-quality and personalized ads on your app, the Publisher Service and unique advertising identifiers help you to work seamlessly with third-party advertising and tracking platforms while protecting user privacy.

Using Huawei Splash Ads displays the ads immediately after an app is launched, even before the home screen of the app is displayed. You need to design a default slogan image for the app in advance, and ensure that the default slogan image is displayed before a splash ad is loaded, enhancing the user experience.

Use Case Description

Express Splash Ads : This Express Splash Ads useful for you to monetize traffic conveniently and improve splash ad experience without requiring you to integrate any SDK.

Development flow

Development Overview

You need to install Android Studio software and possess Native development and I assume that you have prior knowledge about Android Application development.

Hardware Requirement

· A computer (desktop or laptop) running Windows 10.

· Huawei Android phone (with the USB cable), which is used for debugging.

Software Requirement

1. JDK 1.8 or later

2. Android 7.0 or later

3. Android Studio 3.6 or later

4. Gradle 5.0 or later

Preparation

Before you start developing an app, you must complete the following preparations:

  1. Register as a Huawei developer account in AppGallery Connect
  2. Create an app in Android Studio IDE
  3. Generate Signin Certificate Fingrprint and configuring the information in App Gallery Connect.
  4. Enable Data processing Location and download the AGConnectSerices.json file add it App Folder.
  5. You need to add all Ads Kit SDK to your app.

Follow the below steps

Create Android Project

  1. Open Android Studio.
  2. Click NEW Project, select a Project Templet.
  3. Provide New project name and Package Name.
  4. Select Java from the Language drop-down list, and set the Minimum SDK to API 24: Android 7.1 and click Finish.

Modifying Configuration Parameters

Open the build.gradle file in the root directory of your Android Studio project.

Next step is to add the Maven repository:

  1. Choose buildscript > repositories and configure the Maven repository address for the HMS Core SDK.
  2. Choose allprojects > repositories and configure the Maven repository address for the HMS Core SDK.

The following is a configuration code for settings.gradle file:

dependencyResolutionManagement{

repositories {

google()

mavenCentral()

maven { url 'https://developer.huawei.com/repo/' }

}

}

Next step is adding Navi SDK dependency in app-level build.gradle file.

dependencies {

implementation 'androidx.appcompat:appcompat:1.5.1'

implementation 'com.huawei.hms:ads-lite:13.4.56.302'

implementation 'com.huawei.hms:ads-omsdk:1.3.35'

implementation 'com.huawei.hms:ads-consent:3.4.56.300'

implementation 'com.huawei.hms:ads-prime:3.4.56.300'
}

Then add the app gallery plugin in build.gradle file.

apply plugin: 'com.android.application'

apply plugin: 'com.huawei.agconnect'

Development Procedure

  1. Add necessary permission in AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

2. You need to synchronizing user agreement to Huawei Ads by creating an AIDL file for the ExSplashService API and save the file under the com.huawei.hms.ads package, as shown in the following figure.

Save File

3. User agreement in AIDL file is as follows.

interface ExSplashService {
void enableUserInfo(boolean enable);
}

4. In MainActivity OnCreate initialize Ads Kit by declaring the Broadcast Receiver class.

mExSplashBroadcastReceiver = new ExSplashBroadcastReceiver();

IntentFilter filter = new IntentFilter(ACTION_EXSPLASH_DISPLAYED);

registerReceiver(mExSplashBroadcastReceiver, filter);

exSplashService = new ExSplashServiceManager(this);

5. Now set the Express Splash Ads service user information to be enabled or not in user consent.

private void checkUserConsent() {

SharedPreferences preferences = getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);

int status = preferences.getInt(SP_PROTOCOL_KEY, -1);

if (status == -1) { // First launch App

showProtocolDialog();

} else if (status == 0) { // The user does not consent agreement.

exSplashService.enableUserInfo(false);

} else { // The user consent agreement.

exSplashService.enableUserInfo(true);

}

}

6. Create a class that implements Android-native ServiceConnection. Implement the onServiceConnected method of ServiceConnection and call the Android-native ExSplashService.Stub.asInterface method to obtain ExSplashService.

public final class ExSplashServiceConnection implements ServiceConnection {
private static final String TAG = "ExSplashConnection";
private Context context;
public ExSplashServiceConnection(Context context) {
this.context = context;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "onServiceConnected");
exSplashService = ExSplashService.Stub.asInterface(service);
if (exSplashService != null) {
try {
exSplashService.enableUserInfo(enable);
Log.i(TAG, "enableUserInfo done");
} finally {
context.unbindService(this);
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected");
}
}

7. Connect to the AIDL service of express splash ads by calling bindService

private boolean bindService() {

serviceConnection = new ExSplashServiceConnection(context);

Intent intent = new Intent(ACTION_EXSPLASH);

intent.setPackage(PACKAGE_NAME);

boolean result = context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

return result;

}

8. Call the enableUserInfo method to synchronize the user agreement status.

exSplashService = ExSplashService.Stub.asInterface(service);

9. You can use broadcast Express Splash Ads message. 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.

public class ExSplashBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = ExSplashBroadcastReceiver.class.getSimpleName();

@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")) {

Log.i(TAG, "Received the exsplash ad broadcast, action:" + action);

9. Call onDestroy to destroy the services enables by broadcast receiver.

@Override

protected void onDestroy() {

super.onDestroy();

if (mExSplashBroadcastReceiver != null) {

unregisterReceiver(mExSplashBroadcastReceiver);

mExSplashBroadcastReceiver = null;

}

}

10. Run the application and see the Express Splash Ads.

Tips and Tricks

  1. Always use the latest version of the library.
  2. Make sure you have properly enabled all required services in AppGallery Connect.
  3. Make sure dependencies added in build files.

Conclusion

This article will help you to learn the integration of Express Splash Ads from Huawi Ads Kit and its features integration and implementation into your application.

Thank you for reading and if you have enjoyed this article, I would suggest you to implement this and provide your experience.

Output

output

References

Splash Ads Kit:

Express Splash Ads:

Video Course:

FAQ’s :

--

--