Expert: Integration of HMS Core Kits in Expense Android App

Overview

In this article, we will learn how to create an android based Expense App in which I will integrate HMS Core kits such as Huawei ID, Huawei Ads and much more.

In this application, User can record their expensive on daily basis which calculate the whole in/out transaction.

In this series of article, I will cover all the kits with real life usages in this application.

Huawei ID Service Introduction

Huawei ID login provides you with simple, secure, and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authentication, users can just tap the Sign in with HUAWEI ID button to quickly and securely sign in to your app with their HUAWEI IDs.

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.

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.

2. Navigate to Project settings and download the configuration file.

3. Navigate to General Information, and then provide Data Storage location.

App Development

  1. Create A New Project.

2. Configure Project Gradle.

classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
classpath 'com.google.gms:google-services:4.3.5'

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:ads-lite:13.4.30.307'
implementation 'com.huawei.agconnect:agconnect-remoteconfig:1.6.0.300'
implementation 'com.huawei.hms:hianalytics:5.0.3.300'
implementation 'com.huawei.agconnect:agconnect-crash:1.4.1.300'

4. Configure AndroidManifest.

5. Code Implementation

LoginActivity:

package com.hms.expensedemo.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.hms.expensedemo.R;
import com.huawei.agconnect.crash.AGConnectCrash;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.ads.AdListener;
import com.huawei.hms.ads.AdParam;
import com.huawei.hms.ads.InterstitialAd;
import com.huawei.hms.ads.banner.BannerView;
import com.huawei.hms.analytics.HiAnalytics;
import com.huawei.hms.analytics.HiAnalyticsInstance;
import com.huawei.hms.analytics.HiAnalyticsTools;
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 = 1002;
private static String = LoginActivity.class.getName();
private HuaweiIdAuthService mAuthManager;
private HuaweiIdAuthParams mAuthParam;
private BannerView hwBannerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.);
Button view = findViewById(R.id.);
view.setOnClickListener(this);

AGConnectCrash.().enableCrashCollection(false);
//Crash application
AGConnectCrash.().testIt(this);
initAds();
}


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.(, "onAdLoaded");
showInterstitialAd();
}

@Override
public void onAdFailed(int errorCode) {
Log.(, "onAdFailed");
}

@Override
public void onAdOpened() {
Log.(, "onAdOpened");
}

@Override
public void onAdClicked() {
Log.(, "onAdClicked");
}

@Override
public void onAdLeave() {
Log.(, "onAdLeave");
}

@Override
public void onAdClosed() {
Log.(, "onAdClosed");
}
};

}

private void initAds() {
HwAds.init(this);
hwBannerView = findViewById(R.id.huawei_banner_view);
hwBannerView.setVisibility(View.);
AdParam adParam = new AdParam.Builder().build();
hwBannerView.loadAd(adParam);
hwBannerView.setAdListener(adListener);

}

private void signIn() {
mAuthParam = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.)
.setIdToken()
.setAccessToken()
.createParams();
mAuthManager = HuaweiIdAuthManager.(this, mAuthParam);
startActivityForResult(mAuthManager.getSignInIntent(), );
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.) {
signIn();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ) {
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.(data);
if (authHuaweiIdTask.isSuccessful()) {
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
Log.(, huaweiAccount.getDisplayName() + " signIn success ");
Log.(, "AccessToken: " + huaweiAccount.getAccessToken());

Bundle bundle = new Bundle();
bundle.putString(, huaweiAccount.getDisplayName() + " signIn success ");
String eventName = "Login";

bundle.putDouble("ID", 999);
bundle.putLong("Details", 100L);
Analystics.(this).setEvent("login", bundle);

HiAnalyticsInstance instance = HiAnalytics.(this);
HiAnalyticsTools.();


if (instance != null) {
instance.onEvent(eventName, bundle);
}

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("user", huaweiAccount.getDisplayName());
startActivity(intent);
this.finish();

} else {
Log.(, "signIn failed: " + ((ApiException) authHuaweiIdTask.getException()).getStatusCode());
}
}

}
}

MainActivity:

package com.hms.expensedemo.activities;

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;

import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;

import com.hms.expensedemo.R;
import com.hms.expensedemo.adapters.SectionsPageAdapter;
import com.hms.expensedemo.fragments.BalanceFragment;
import com.hms.expensedemo.fragments.CustomBottomSheetDialogFragment;
import com.hms.expensedemo.fragments.ExpenseFragment;

public class MainActivity extends AppCompatActivity {

private ViewPager mViewPager;

public static FloatingActionButton ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.);


mViewPager=findViewById(R.id.);
setupViewPager(mViewPager);

TabLayout tabLayout=findViewById(R.id.);
tabLayout.setupWithViewPager(mViewPager);


= (FloatingActionButton) findViewById(R.id.);
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog");

}
});

}




private void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter=new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new ExpenseFragment(),"Expenses");
adapter.addFragment(new BalanceFragment(),"Balance");
viewPager.setAdapter(adapter);
}



}

App Build Result

Tips and Tricks

1. Identity Kit displays the HUAWEI ID registration or sign-in page first. The user can use the functions provided by Identity Kit only after signing in using a registered HUAWEI ID.

2. 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.

3. Ensure that the agconnect-services.json file is the original one without any modification.

Conclusion

In this article, we have learned how to integrate Huawei ID and Ads in Android application. After completely read this article user can easily implement Huawei ID and Ads in the 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/introduction-0000001050048870

--

--

Android Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store