[Huawei Developers] ask011:
Introduction

Huawei provides various services for developers to come up with the solution for the problems which we are facing in our everyday life.

Problem: We have researched and found out that some people do have sight problems due to age and because of that they cannot read the text on the screen clearly, but they love to read news every day. Keeping that in mind we come up with a solution.

Solution: Using Huawei Mobile Service (HMS) core kits, we have tried to integrate as many kits as possible and created a smart but very effective news application.

In this News Application user can login, read or listen to the latest news and also user can change the default app language to their own native language. User will also receive notification messages based on the topic they subscribed on regular basis.

Let’s look into the Kits we have integrated in our Smart News App:

1. Account Kit

2. Ads Kit

3. ML Kit

4. Push Kit

5. Crash Service

This application will be explained in two parts:

1. In part one, I will explain how to integrate Account Kit and Ads Kit.

2. In part two, I will explain how to integrate ML Kit and Push Kit.

Demo

We will be focusing only on Account Kit and Ads Kit demo in this part.

Prerequisites

1. Must have a Huawei Developer Account.

2. Must have a Huawei phone with HMS 4.0.0.300 or later

3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

Integration Preparations

1. First we need to create a project in android studio.

2. Generate a SHA-256 certificate fingerprint. To generate SHA-256 certificate fingerprint use below command.

keytool -list -v -keystore
D:\SmartNewsApp\file_name.keystore
-alias alias_name.

3. Create a project in AppGallery Connect.

4. Enable Account kit and Ads Kit in Manage APIs section.

5. Provide the SHA Key in App Information Section.

6. Provide storage location.

7. Download the agconnect-services.json, copy and paste the Json file in the app folder.

8. Copy and paste the below maven URL inside the repositories of buildscript and allprojects (project build.gradle file).

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

9. Copy and paste the below plugin in the app build.gradle file.

apply plugin: 'com.huawei.agconnect'

10. After that, we need to add dependencies into build.gradle files.

implementation 'com.huawei.hms:hwid:5.0.3.301' //Account Kit
implementation 'com.huawei.hms:ads-lite:13.4.34.301' //Ads Kit

Now, we need to sync our build.gradle files.

Account Kit Overview

HUAWEI Account Kit provides developers with simple, secure, and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authorization, users can just tap the Sign In with HUAWEI ID button to quickly and securely sign in to the app.

We will implement authorization code sign in use case for login scenario in this application.

Let’s start the coding!

Signing with Authorization Code

Add the below code in activity_main.xml.

<com.huawei.hms.support.hwid.ui.HuaweiIdAuthButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hwi_sign_in"
android:layout_gravity="center_horizontal|center_vertical"
android:visibility="visible"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="#3b5998"
app:hwid_button_theme="hwid_button_theme_full_title"
app:hwid_corner_radius="hwid_corner_radius_small"/>

Huawei id auth button component is used here. Please check for getting more information about usage of Huawei ID signing in button.

Add the below code in LoginActivity.java

HuaweiIdAuthParams authParams = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setAuthorizationCode().setEmail().createParams();
HuaweiIdAuthService service = HuaweiIdAuthManager.getService(MainActivity.this, authParams);
startActivityForResult(service.getSignInIntent(), Constants.HUAWEI_SIGNIN);

When we click the Huawei ID signing in button, it needs the HuaweiIdAuthParams and create a service with authParams. Then, we call startActivityForResult() method in Huawei ID signing in button click with service and HUAWEI_SIGNIN constant. The constant (HUAWEI_SIGNIN) uses for handling the requestCode in onActivityResult() method. When the user clicks login with Huawei ID button, the app needs to authorize and login operations from the user.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
// Process the authorization result and obtain the authorization code from AuthHuaweiId.
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_CODE) {
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.parseAuthResultFromIntent(data);
if (authHuaweiIdTask.isSuccessful()) {
// The sign-in is successful, and the user's HUAWEI ID information and authorization code are obtained.
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
String name = huaweiAccount.getDisplayName();
String email = huaweiAccount.getEmail();
SharedPreferences.Editor editor = getSharedPreferences(Constants.MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putBoolean("login", true);
editor.putString("name", name);
editor.putString("email", email);
editor.apply();
editor.commit();
Intent intent = new Intent(MainActivity.this, NewsActivity.class);
startActivity(intent);

} else {
// The sign-in failed.
Log.e(TAG, getApplication().getResources().getString(R.string.sigin_failed));
Toast.makeText(this, getApplicationContext().getResources().getString(R.string.unable_to_login), Toast.LENGTH_LONG).show();
}
}
}

When the user is signed in successfully and we could able to get users name and profile picture, email address and etc. It is displayed in the user profile.

Add the below code in UserProfile.java class.

signout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HuaweiIdAuthParams authParams = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setIdToken().createParams();
HuaweiIdAuthService service= HuaweiIdAuthManager.getService(About.this, authParams) ;
Task<Void> signOutTask = service.signOut();
signOutTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
// Processing after the sign-out.
finishAndRemoveTask();
}
});
}
});

Ads Kits Overview

The most common method used by mobile developers is to generate revenue from their application is to create advertising spaces for advertisers. Advertisers prefer to place their ads through mobile media rather than printed publications.In this sense, Huawei Ads meets the requirement of both advertisers and mobile developers.

HMS Ads Kit is a mobile service that helps us to create high quality and personalized ads in our application.

Currently, HMS contains 7 kind of ads kits. In this application we will be integrating Banner ads.

Adding a Banner Ad

Add the following lines to your app level build.gradle file.

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

Add BannerView to the XML layout file. Set the hwads:adId for the ad slot ID and hwads:bannerSize for the ad size.

<com.huawei.hms.ads.banner.BannerView android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/hw_banner_view"
app:bannerSize="BANNER_SIZE_360_57" app:adId="testw6vs28auh3"
android:layout_alignParentBottom="true"/>

Note: Various banner size is available.

After creating the BannerView in XML file, call it from your class and load the banner ad. In this application we are displaying banner ads after every news in the newslist. So we have added in NewsListAdapter class.

Add the below code in NewsListAdapter class.

BannerView bottomBannerView = itemView.findViewById(R.id.hw_banner_view);
rootView = itemView.findViewById(R.id.root_view);

AdParam adParam = new AdParam.Builder().build();
RelativeLayout bottomBannerView.loadAd(adParam);

// Call new BannerView(Context context) to create a BannerView class.
BannerView topBannerView = new BannerView(context);
topBannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_360_57);
topBannerView.loadAd(adParam);
rootView.addView(topBannerView);

Tips and Tricks

1. Add agconnect-services.json file without fail.

2. Add SHA-256 fingerprint without fail.

3. Make sure dependencies added in build files.

4. Banner ads be can also added programmatically.

Conclusion

You may find some problems in your surroundings and I hope you can come up with a solution for those problems using HMS core kits.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

HMS Account Kit

HMS Ads Kit

Read In Forum