[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100037.06020670511892212347592868954745:50510727065843:2800:60F87E2B02F69F6A79698C058E51465D000514FC7406DA750ED4884ABF70A5C0.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100037.06020670511892212347592868954745:50510727065843:2800:60F87E2B02F69F6A79698C058E51465D000514FC7406DA750ED4884ABF70A5C0.gif[/IMG]​

What is it?

Huawei Games is a group of APIs from Huawei to simplify some games basic features like leaderboards, achievements, Events and online matches.

Gaming technologies are constantly evolving. Nevertheless, a lot of core gameplay elements have remained unchanged for decades. High scores, leaderboards, quests, achievements, and multiplayer support are examples. If you are developing a game for the Android platform, you don't have to implement any of those elements manually. You can simply use the Huawei Game services APIs instead.

Features of Huawei Game services

· HUAWEI ID sign-in

· Real-name authentication

· Bulletins

· Achievements

· Events

· Leaderboard

· Saved games

· Player statistics

Let’s start!

You should follow the steps

1) Create an android project in android studio.

2) Get the SHA Key. Create SHA key

3) Create a Game App in the Huawei app galley connect (AGC)

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100105.38730048068101176697429581171590:50510727065843:2800:8F1AACC020B75E882E10E1D3A0416B1CC802F105E4FE01CB3BEFE3F4D598DCC4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100105.38730048068101176697429581171590:50510727065843:2800:8F1AACC020B75E882E10E1D3A0416B1CC802F105E4FE01CB3BEFE3F4D598DCC4.png[/IMG]​

4) Provide the SHA key in the information section.

5) Provide storage location.

6) Enable Game service.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100132.19358514246706197041998377929478:50510727065843:2800:E2ADC566F18716668489EAFCC0D277A92EF0283A08E40EB2D37F922A69F70915.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100132.19358514246706197041998377929478:50510727065843:2800:E2ADC566F18716668489EAFCC0D277A92EF0283A08E40EB2D37F922A69F70915.png[/IMG]​

7) After all the steps need to download the agconnect-services.json from the app information section. Copy and paste the json file in the app folder of the android project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100144.85672670586706896455127770018671:50510727071118:2800:FD45DD4BB18EF44D46AE7710F7D36C3A2CCBE8BD8888562F7CD7CB15A2070A56.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100144.85672670586706896455127770018671:50510727071118:2800:FD45DD4BB18EF44D46AE7710F7D36C3A2CCBE8BD8888562F7CD7CB15A2070A56.png[/IMG]​

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/'

Add the classpath in the dependencies path

classpath 'com.huawei.agconnect:agcp:1.2.1.300'

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

apply plugin: 'com.huawei.agconnect'

Add the below dependencies.

implementation 'com.huawei.hms:base:4.0.4.301'

implementation 'com.huawei.hms:hwid:4.0.4.300'

implementation 'com.huawei.hms:iap:4.0.4.300'

implementation 'com.huawei.hms:game:4.0.3.301'

Note: if you are not using the In-App purchase then no need add the following dependency.

1. implementation 'com.huawei.hms:iap:4.0.4.300'

10) Sync App.

In this series of article we will build Tic Tac Toe game. In this article will see the following features.

· Sign In.

· Initialization.

· Getting player info.

· Saving player info.

Sign In

User can sign-in with Huawei account. They can play game.

public void signIn() {
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.getService(this, getHuaweiIdParams()).silentSignIn();
authHuaweiIdTask.addOnSuccessListener(new OnSuccessListener<AuthHuaweiId>() {
@Override
public void onSuccess(AuthHuaweiId authHuaweiId) {
showLog("signIn success");
showLog("display:" + authHuaweiId.getDisplayName());
welcomeTv.setVisibility(View.VISIBLE);
welcomeTv.setText("Welcome back " + authHuaweiId.getDisplayName());
signIn.setVisibility(View.INVISIBLE);
showLog("AT:" + authHuaweiId.getAccessToken());
mAuthid = authHuaweiId;
SignInCenter.get().updateAuthHuaweiId(authHuaweiId);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
if (e instanceof ApiException) {
signIn.setVisibility(View.VISIBLE);
welcomeTv.setVisibility(View.INVISIBLE);
ApiException apiException = (ApiException) e;
showLog("signIn failed:" + apiException.getStatusCode());
showLog("start getSignInIntent");
signInNewWay();
}
}
});
}

public HuaweiIdAuthParams getHuaweiIdParams() {
return new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM_GAME).createParams();
}

public void signInNewWay() {
Intent intent = HuaweiIdAuthManager.getService(SignInActivity.this, getHuaweiIdParams()).getSignInIntent();
startActivityForResult(intent, SIGN_IN_INTENT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (SIGN_IN_INTENT == requestCode) {
handleSignInResult(data);
} else {
showLog("unknown requestCode in onActivityResult");
}
}
private void handleSignInResult(Intent data) {
if (null == data) {
showLog("signIn inetnt is null");
return;
}
// HuaweiIdSignIn.getSignedInAccountFromIntent(data);
String jsonSignInResult = data.getStringExtra("HUAWEIID_SIGNIN_RESULT");
if (TextUtils.isEmpty(jsonSignInResult)) {
showLog("SignIn result is empty");
return;
}
try {
HuaweiIdAuthResult signInResult = new HuaweiIdAuthResult().fromJson(jsonSignInResult);
if (0 == signInResult.getStatus().getStatusCode()) {
showLog("Sign in success.");
showLog("Sign in result: " + signInResult.toJson());
SignInCenter.get().updateAuthHuaweiId(signInResult.getHuaweiId());
} else {
showLog("Sign in failed: " + signInResult.getStatus().getStatusCode());
}
} catch (JSONException var7) {
showLog("Failed to convert json from signInResult.");
}
}

The above show the sign in with Huawei account.

Initialization

Key steps to launch the game

Add the following code to the onCreate method of the Application to register the callback listening function of the activity.

package com.android.huawei.tictactoe;

import android.app.Application;

import com.huawei.hms.api.HuaweiMobileServicesUtil;

public class TicTacToeApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
HuaweiMobileServicesUtil.setApplication(this);
}
}

Call JosApps.getJosAppsClient to initialize the JosAppsClient object and JosAppsClient.init to initialize the game.

private void init() {
JosAppsClient appsClient = JosApps.getJosAppsClient(this, null);
appsClient.init();
Log.i(TAG, "init success");
}

Getting player info

public void getCurrentPlayer() {
PlayersClientImpl client = (PlayersClientImpl) Games.getPlayersClient(this, getAuthHuaweiId());

Task<Player> task = client.getCurrentPlayer();
task.addOnSuccessListener(new OnSuccessListener<Player>() {
@Override
public void onSuccess(Player player) {
String result = "display:" + player.getDisplayName() + "\n"
+ "playerId:" + player.getPlayerId() + "\n"
+ "playerLevel:" + player.getLevel() + "\n"
+ "timestamp:" + player.getSignTs() + "\n"
+ "playerSign:" + player.getPlayerSign();
showLog(result);
playerId = player.getPlayerId();

}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
if (e instanceof ApiException) {
String result = "rtnCode:" + ((ApiException) e).getStatusCode();
showLog(result);
}
}
});
}

The above code gives the player info like playerId, Player name, player level, in which level he/she is playing.

Saving player info.

Player info can be updated using AppPlayerInfo. Player level, role, rank etc. The following code show the saving player info.

To save player info playerId is mandatory.

public void savePlayerInfo() {
if (TextUtils.isEmpty(playerId)) {
Toast.makeText(this, "Get the current user first", Toast.LENGTH_SHORT).show();
return;
}
PlayersClient client = Games.getPlayersClient(this, getAuthHuaweiId());
AppPlayerInfo appPlayerInfo = new AppPlayerInfo();
appPlayerInfo.area = "2";
appPlayerInfo.rank = "56";
appPlayerInfo.role = "Pro";
appPlayerInfo.sociaty = "Red Cliff III";
appPlayerInfo.playerId = playerId;
Task<Void> task = client.savePlayerInfo(appPlayerInfo);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void v) {
Toast.makeText(SignInActivity.this, "Player info saved successfully ", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
if (e instanceof ApiException) {
String result = "rtnCode:" + ((ApiException) e).getStatusCode();
Toast.makeText(SignInActivity.this, result, Toast.LENGTH_SHORT).show();

}
}
});
}

Result

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100423.92898096181148418208920066663613:50510727065843:2800:852C7E58E956476EE80F552CA04A5D265D2C471CC6A1FC76655E39988181C934.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100423.92898096181148418208920066663613:50510727065843:2800:852C7E58E956476EE80F552CA04A5D265D2C471CC6A1FC76655E39988181C934.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100432.82164411163465142907694347128235:50510727065843:2800:C9E8C7B874AD73863BC6EF57BE915B90B67876EBD9777007F63D8A6954133C76.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/982/2640091000001768982.20200609100432.82164411163465142907694347128235:50510727065843:2800:C9E8C7B874AD73863BC6EF57BE915B90B67876EBD9777007F63D8A6954133C76.png[/IMG]​