Overview

HUAWEI Identity Kit provides unified address management services for users and allows your app to access users' addresses conveniently upon obtaining permissions from the users.

What You Will Create

In this codelab, you will use the demo project that has been created for you to call HUAWEI Identity Kit APIs. Through the demo project, you will:

What You Will Learn

Hardware Requirements

Software Requirements

HUAWEI HMS Core integration requires the following preparations

For details, see the HUAWEI HMS Core Integration Preparation.

Downloading the Code

Download and decompress CodeLab_Identity_Demo.zip.

CodeLab_Identity_Demo

Opening a Project in Android Studio

  1. Start Android Studio.
  2. Go to File > import project and select the path of the extracted source code.
  3. Click Finish.

Adding the AppGallery Connect Configuration File of Your App

Configuring the HUAWEI Ads Kit SDK Repository and Dependency Package

Configure the dependency package in the app-level build.gradle file.

build.gradle

dependencies { ... implementation 'com.huawei.hms:identity:4.0.0.300' ... }

Configure the obfuscation script in the app/proguard-rules.pro file.

proguard-rules.pro

-ignorewarnings -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;}

Click Sync Now to synchronize the project.

Implementing the Demo

Add the code for querying user addresses and opening the user address selection page to the MainActivity.java class.

private void getUserAddress() { UserAddressRequest req = new UserAddressRequest(); Task<GetUserAddressResult> task = Address.getAddressClient(this).getUserAddress(req); task.addOnSuccessListener(new OnSuccessListener<GetUserAddressResult>() { @Override public void onSuccess(GetUserAddressResult result) { Log.i(TAG, "onSuccess result code:" + result.getReturnCode()); try { startActivityForResult(result); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { Log.i(TAG, "on Failed result code:" + e.getMessage()); } }); } private void startActivityForResult(GetUserAddressResult result) throws IntentSender.SendIntentException { Status status = result.getStatus(); if (result.getReturnCode() == 0 && status.hasResolution()) { Log.i(TAG, "the result had resolution."); status.startResolutionForResult(this, GET_ADDRESS); } else { Log.i(TAG, "the response is wrong, the return code is " + result.getReturnCode()); } }

Add the tap event of the user address query button to the end of the OnCreate method in the MainActivity.java file.

queryAddrButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getUserAddress(); } });

In the MainActivity.java file, override the onActivityResult logic for parsing user address selection, returning the selected user address, and displaying the selected user address in the text box on the device.

@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.i(TAG, "onActivityResult requestCode " + requestCode + " resultCode " + resultCode); switch (requestCode) { case GET_ADDRESS: switch (resultCode) { case Activity.RESULT_OK: UserAddress userAddress = UserAddress.parseIntent(data); if (userAddress != null) { StringBuilder sb = new StringBuilder(); sb.append("name:" + userAddress.getName() + ","); sb.append("city:" + userAddress.getAdministrativeArea() + ","); sb.append("area:" + userAddress.getLocality() + ","); sb.append("address:" + userAddress.getAddressLine1() + userAddress.getAddressLine2() + ","); sb.append("phone:" + userAddress.getPhoneNumber()); Log.i(TAG, "user address is " + sb.toString()); textView.setText(sb.toString()); } else { textView.setText("Failed to get user address."); } break; case Activity.RESULT_CANCELED: break; default: Log.i(TAG, "result is wrong, result code is " + resultCode); break; } default: break; } }

Test

Tap to run the project you have created in Android Studio to generate an APK. Then install the APK on the test phone. You will see the graphical interface shown in the following figures.

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

For more information, please click the following links:
HUAWEI Identity SDK Development Guide

API Reference

FAQ

You can download the source code in github.com/Huawei/Consumer/tree/master/Codelabs/IdentityKit

You can also click the button below to download the source code.

Download

Code copied