Expert: Integration of HMS Core Kits in MVVM and RxAndroid based Android App Part-4

Manoj Kumar
4 min readOct 14, 2021

--

Overview

In this article, I will create a Movie Show android application in which I will integrate HMS Core kits such as Huawei ID, Analytics, Huawei Ads and much more.

In this article, I will integrate Full screen ads.

In this series of article, I will cover all the kits with real life usages in Movie Show application. This is the part-4 article of this series.

Part-1: https://forums.developer.huawei.com/forumPortal/en/topic/0202684555189490078?fid=0101187876626530001

Part-2: https://forums.developer.huawei.com/forumPortal/en/topic/0201690592255120129?fid=0101187876626530001

Part-3: https://forums.developer.huawei.com/forumPortal/en/topic/0201690596679190130?fid=0101188387844930001

Interstitial Ads Introduction

Interstitial ads are full-screen ads that covers the interface of an app. Such as ad is displayed when a user starts, pauses, or exits an app, without disrupting the user’s experience.

Integration process

1. Add following dependency in gradle file.

implementation 'com.huawei.hms:ads-lite:13.4.30.307'Copy code

2. Create Instance for InterstitialAd.

interstitialAd = new InterstitialAd(getActivity()); interstitialAd.setAdId("testb4znbuh3n2"); AdParam adParam = new AdParam.Builder().build(); interstitialAd.loadAd(adParam); interstitialAd.setAdListener(adListener); interstitialAd.show();  private AdListener adListener = new AdListener() {     @Override     public void onAdLoaded() {         Log.d(TAG, "onAdLoaded");         showInterstitialAd();     }     @Override     public void onAdFailed(int errorCode) {         Log.d(TAG, "onAdFailed");     }     @Override     public void onAdOpened() {         Log.d(TAG, "onAdOpened");     }     @Override     public void onAdClicked() {         Log.d(TAG, "onAdClicked");     }     @Override     public void onAdLeave() {         Log.d(TAG, "onAdLeave");     }     @Override     public void onAdClosed() {         Log.d(TAG, "onAdClosed");     } };Copy code

Prerequisite

  1. Huawei Phone EMUI 3.0 or later.
  2. Non-Huawei phones Android 4.4 or later (API level 19 or higher).
  3. HMS Core APK 4.0.0.300 or later.
  4. Android Studio
  5. AppGallery Account

App Gallery Integration process

  1. Sign In and Create or Choose a project on AppGallery Connect portal.
  1. Navigate to Project settings and download the configuration file.
  1. Navigate to General Information, and then provide Data Storage location.
  1. ​​​​​​​

App Development

  1. Create A New Project.

2. Configure Project Gradle.

  • // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() maven { url 'http://developer.huawei.com/repo/' } } dependencies { classpath 'com.android.tools.build:gradle:4.0.1' classpath 'com.huawei.agconnect:agcp:1.2.1.301' } } allprojects { repositories { google() jcenter() maven { url 'http://developer.huawei.com/repo/' } } } task clean(type: Delete) { delete rootProject.buildDir }

3. Configure App Gradle.

  • //HMS Kits api 'com.huawei.hms:dynamicability:1.0.11.302' implementation 'com.huawei.agconnect:agconnect-auth:1.4.1.300' implementation 'com.huawei.hms:hwid:5.3.0.302' implementation 'com.huawei.hms:hianalytics:5.0.1.301' implementation 'com.huawei.agconnect:agconnect-crash:1.4.1.300' implementation 'com.huawei.hms:ads-lite:13.4.30.307' implementation 'com.huawei.hms:ads-lite:13.4.30.307'

4. Configure AndroidManifest.xml.

  • <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

5. Create Activity class with XML UI.

LoginActivity:

package com.hms.manoj.aab;import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;import com.hms.manoj.Analystics;
import com.hms.manoj.aab.ui.ShowsActivity;
import com.hms.movieshow.R;
import com.huawei.agconnect.crash.AGConnectCrash;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.ads.AdParam;
import com.huawei.hms.ads.HwAds;
import com.huawei.hms.ads.banner.BannerView;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.support.hwid.HuaweiIdAuthManager;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParams;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParamsHelper;
import com.huawei.hms.support.hwid.result.AuthHuaweiId;
import com.huawei.hms.support.hwid.service.HuaweiIdAuthService;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener { private static final int REQUEST_SIGN_IN_LOGIN = 1002;
private static String TAG = LoginActivity.class.getName();
private HuaweiIdAuthService mAuthManager;
private HuaweiIdAuthParams mAuthParam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button view = findViewById(R.id.btn_sign);
view.setOnClickListener(this);
initAds();
AGConnectCrash.getInstance().enableCrashCollection(true);
//Crash application
AGConnectCrash.getInstance().testIt(this);
}
private BannerView hwBannerView; private void loadFullScreenAds(){
interstitialAd = new InterstitialAd(getActivity());
interstitialAd.setAdId("testb4znbuh3n2");
AdParam adParam = new AdParam.Builder().build();
interstitialAd.loadAd(adParam);
interstitialAd.setAdListener(adListener); interstitialAd.show(); private AdListener adListener = new AdListener() {
@Override
public void onAdLoaded() {
Log.d(TAG, "onAdLoaded");
showInterstitialAd();
}
@Override
public void onAdFailed(int errorCode) {
Log.d(TAG, "onAdFailed");
}
@Override
public void onAdOpened() {
Log.d(TAG, "onAdOpened");
}
@Override
public void onAdClicked() {
Log.d(TAG, "onAdClicked");
}
@Override
public void onAdLeave() {
Log.d(TAG, "onAdLeave");
}
@Override
public void onAdClosed() {
Log.d(TAG, "onAdClosed");
}
};
} private void initAds() {
HwAds.init(this);
hwBannerView = findViewById(R.id.huawei_banner_view);
hwBannerView.setVisibility(View.VISIBLE);
AdParam adParam = new AdParam.Builder().build();
hwBannerView.loadAd(adParam);
hwBannerView.setAdListener(adListener);
} private void signIn() {
mAuthParam = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken()
.setAccessToken()
.createParams();
mAuthManager = HuaweiIdAuthManager.getService(this, mAuthParam);
startActivityForResult(mAuthManager.getSignInIntent(), REQUEST_SIGN_IN_LOGIN);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_sign) {
signIn();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SIGN_IN_LOGIN) {
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.parseAuthResultFromIntent(data);
if (authHuaweiIdTask.isSuccessful()) {
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
Log.i(TAG, huaweiAccount.getDisplayName() + " signIn success ");
Log.i(TAG, "AccessToken: " + huaweiAccount.getAccessToken());
Bundle bundle = new Bundle();
bundle.putString(TAG,huaweiAccount.getDisplayName() + " signIn success ");
Analystics.getInstance(this).setEvent("login",bundle);
Intent intent = new Intent(this, ShowsActivity.class);
intent.putExtra("user", huaweiAccount.getDisplayName());
startActivity(intent);
this.finish();
} else {
Log.i(TAG, "signIn failed: " + ((ApiException) authHuaweiIdTask.getException()).getStatusCode());
}
}
}
}

App Build Result

​​​​​​​

Tips and Tricks

If you are using a device of the Chinese mainland version, which is connected to the Internet in the Chinese mainland, only these two banner ad dimensions are supported. All dimensions are supported outside the Chinese mainland. To connect to an environment outside the Chinese mainland for testing, you need to use a device version for outside the Chinese mainland and connect to the Internet outside the Chinese mainland.

Conclusion

In this article, we have learned how to integrate Full screen Ads in Android application. After completely read this article, user can easily implement ads kit in the android based application.

Thanks for reading this article. Be sure to like and comment to this article, if you found it helpful. It means a lot to me.

References

HMS Docs:

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/publisher-service-interstitial-0000001050064970

--

--

Manoj Kumar
Manoj Kumar

No responses yet