{{announcement.body}}
{{announcement.title}}

HMS Game Service — Sign-in Function [Java]

DZone 's Guide to

HMS Game Service — Sign-in Function [Java]

In this article, I would like to address the Game Service topic by doing a practical example in which we will implement the kit.

· Integration Zone ·
Free Resource

Introduction

In this article, I would like to address the Game Service topic by doing a practical example in which we will implement the kit. The goal is to achieve a simple application where our user can log in with his Huawei ID and obtain information regarding his player on Game Service. In the Huawei repositories, we can find projects with all the implementation but in my opinion, it is better to create a new project where we have the opportunity to do our development.

Steps:

1. Create an App in AGC

2. Add the necessary libraries and repositories

3. Permissions in our Application

4. Building the user interface

5. Create the Signing class

6. Write the code in our MainActity

7. Test the App

Create an App in AGC

If you already have experience implementing HMS you will have noticed that the creation of an App in the AGC console is regularly required. If you are in this case, I recommend that you skip this step and go to step 3.

In this link, you can find a detailed guide on how to create an App in AGC, generate your fingerprint, and download the JSON services.

https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#0

In case you do not have Android Studio Configured on your device, I also share a guide with the requirements.

https://developer.huawei.com/consumer/en/codelab/HMSAccounts-Kotlin/index.html#1

Once your App is created in AGC, you must activate the Game Service, Account Kit, and In-App purchases services in case you require it. We can go to the Apis Management tab

What Did We Accomplish So Far?

We have an App in AGC connected to our project and with activated services.

Add the Necessary Libraries and Repositories

Once our project is created in Android Studio it will be necessary to add the following lines of code

Let's add the following lines to the project Gradle.

Java
 




x
45


 
1
buildscript {
2
 
          
3
    repositories {
4
 
          
5
        google()
6
 
          
7
        jcenter()
8
 
          
9
                  //Esta linea
10
 
          
11
        maven { url 'http://developer.huawei.com/repo/' }
12
 
          
13
    }
14
 
          
15
    dependencies {
16
 
          
17
        classpath "com.android.tools.build:gradle:4.0.0"
18
 
          
19
        //Tambien esta esta linea
20
 
          
21
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
22
 
          
23
    }
24
 
          
25
}
26
 
          
27
  
28
 
          
29
allprojects {
30
 
          
31
     repositories {
32
 
          
33
         google()
34
 
          
35
         jcenter()
36
 
          
37
         //Repositorio de Maven
38
 
          
39
    maven {url 'http://developer.huawei.com/repo/'}
40
 
          
41
     }
42
 
          
43
 }
44
 
          
45
 
          



Now let's add the necessary dependencies in the app Gradle.

implementation'com.huawei.agconnect:agconnect-core:1.3.1.300' //HMSCore

 implementation 'com.huawei.hms:hwid:4.0.4.300'   //Huawei Id

 implementation 'com.huawei.hms:game:4.0.3.301'   //Game Service

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

 implementation 'com.squareup.picasso:picasso:2.71828'

Do not forget to add the plugin.

apply plugin:'com.huawei.agconnect'

Permissions in our Application

Now let's add the permissions of our Application because we want to use the Game Service services. So in the Android Manifest File, let's add the necessary permissions.

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

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

Building the User Interface

The time has come to build the user interface, what we will do is use the powerful tool that Android Studio from Constraint offers us, basically what we are looking for is to achieve something like this. Where we will place the elements that we want to show when obtaining the data of our user. Of course, you can create the interface that you like the most, but if you want to use this simple distribution of elements this is the code

XML
 




xxxxxxxxxx
1
189


 
1
<?xml version="1.0" encoding="utf-8"?>
2
 
          
3
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
4
 
          
5
    xmlns:app="http://schemas.android.com/apk/res-auto"
6
 
          
7
    xmlns:tools="http://schemas.android.com/tools"
8
 
          
9
    android:layout_width="match_parent"
10
 
          
11
    android:layout_height="match_parent"
12
 
          
13
    tools:context=".MainActivity">  
14
 
          
15
    <TextView
16
 
          
17
        android:id="@+id/textView"
18
 
          
19
        android:layout_width="wrap_content"
20
 
          
21
        android:layout_height="wrap_content"
22
 
          
23
        android:layout_marginTop="68dp"
24
 
          
25
        android:text="Welcome"
26
 
          
27
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
28
 
          
29
        app:layout_constraintEnd_toEndOf="parent"
30
 
          
31
        app:layout_constraintStart_toStartOf="parent"
32
 
          
33
        app:layout_constraintTop_toTopOf="parent" />
34
 
          
35
  
36
 
          
37
    <ImageView
38
 
          
39
        android:id="@+id/avatarImg"
40
 
          
41
        android:layout_width="wrap_content"
42
 
          
43
        android:layout_height="wrap_content"
44
 
          
45
        android:layout_marginTop="36dp"
46
 
          
47
        app:layout_constraintEnd_toEndOf="parent"
48
 
          
49
        app:layout_constraintHorizontal_bias="0.498"
50
 
          
51
        app:layout_constraintStart_toStartOf="parent"
52
 
          
53
        app:layout_constraintTop_toBottomOf="@+id/textView"
54
 
          
55
        tools:srcCompat="@tools:sample/avatars" />
56
 
          
57
  
58
 
          
59
    <TextView
60
 
          
61
        android:id="@+id/textView9"
62
 
          
63
        android:layout_width="wrap_content"
64
 
          
65
        android:layout_height="wrap_content"
66
 
          
67
        android:layout_marginStart="28dp"
68
 
          
69
        android:text="Player Id"
70
 
          
71
        app:layout_constraintStart_toStartOf="parent"
72
 
          
73
        app:layout_constraintTop_toTopOf="@+id/idtv" />
74
 
          
75
  
76
 
          
77
    <TextView
78
 
          
79
        android:id="@+id/textView10"
80
 
          
81
        android:layout_width="wrap_content"
82
 
          
83
        android:layout_height="wrap_content"
84
 
          
85
        android:layout_marginTop="32dp"
86
 
          
87
        android:layout_marginEnd="51dp"
88
 
          
89
        android:text="Player Level"
90
 
          
91
        app:layout_constraintEnd_toStartOf="@+id/leveltv"
92
 
          
93
        app:layout_constraintHorizontal_bias="0.0"
94
 
          
95
        app:layout_constraintStart_toStartOf="@+id/textView9"
96
 
          
97
        app:layout_constraintTop_toBottomOf="@+id/textView9" />
98
 
          
99
  
100
 
          
101
    <TextView
102
 
          
103
        android:id="@+id/idtv"
104
 
          
105
        android:layout_width="wrap_content"
106
 
          
107
        android:layout_height="wrap_content"
108
 
          
109
        android:layout_marginStart="40dp"
110
 
          
111
        android:layout_marginTop="24dp"
112
 
          
113
        android:text="TextView"
114
 
          
115
        app:layout_constraintEnd_toEndOf="parent"
116
 
          
117
        app:layout_constraintHorizontal_bias="0.082"
118
 
          
119
        app:layout_constraintStart_toEndOf="@+id/textView9"
120
 
          
121
        app:layout_constraintTop_toBottomOf="@+id/avatarImg" />
122
 
          
123
  
124
 
          
125
    <TextView
126
 
          
127
        android:id="@+id/leveltv"
128
 
          
129
        android:layout_width="wrap_content"
130
 
          
131
        android:layout_height="wrap_content"
132
 
          
133
  
134
 
          
135
        android:layout_marginTop="32dp"
136
 
          
137
        android:text="TextView"
138
 
          
139
        app:layout_constraintStart_toStartOf="@+id/idtv"
140
 
          
141
        app:layout_constraintTop_toBottomOf="@+id/idtv" />
142
 
          
143
  
144
 
          
145
    <Button
146
 
          
147
        android:id="@+id/loginButton"
148
 
          
149
        android:layout_width="match_parent"
150
 
          
151
        android:layout_height="wrap_content"
152
 
          
153
        android:layout_marginBottom="16dp"
154
 
          
155
        android:text="Login"
156
 
          
157
        app:layout_constraintBottom_toBottomOf="parent"
158
 
          
159
        tools:layout_editor_absoluteX="0dp" />
160
 
          
161
  
162
 
          
163
    <Button
164
 
          
165
        android:id="@+id/btnInfo"
166
 
          
167
        android:layout_width="wrap_content"
168
 
          
169
        android:layout_height="wrap_content"
170
 
          
171
        android:layout_marginTop="88dp"
172
 
          
173
        android:text="PlayerInfo"
174
 
          
175
        app:layout_constraintBottom_toTopOf="@+id/loginButton"
176
 
          
177
        app:layout_constraintEnd_toEndOf="parent"
178
 
          
179
        app:layout_constraintHorizontal_bias="0.475"
180
 
          
181
        app:layout_constraintStart_toStartOf="parent"
182
 
          
183
        app:layout_constraintTop_toBottomOf="@+id/textView10"
184
 
          
185
        app:layout_constraintVertical_bias="0.178" />
186
 
          
187
  
188
 
          
189
</androidx.constraintlayout.widget.ConstraintLayout>



Create the SignInCenter class

In this class, we will make the instance of the class and we will be able to manage the Huawei of Authentication

Java
 




xxxxxxxxxx
1
33


 
1
public class SignInCenter {
2
 
          
3
    private static SignInCenter INS = new SignInCenter();
4
 
          
5
  
6
 
          
7
    private static AuthHuaweiId currentAuthHuaweiId;
8
 
          
9
  
10
 
          
11
    public static SignInCenter get() {
12
 
          
13
        return INS;
14
 
          
15
    }
16
 
          
17
  
18
 
          
19
    public void updateAuthHuaweiId(AuthHuaweiId AuthHuaweiId) {
20
 
          
21
        currentAuthHuaweiId = AuthHuaweiId;
22
 
          
23
    }
24
 
          
25
  
26
 
          
27
    public AuthHuaweiId getAuthHuaweiId() {
28
 
          
29
        return currentAuthHuaweiId;
30
 
          
31
    }
32
 
          
33
}



Write the Code in our MainActity

Let's work on our Main Activity is the time to get our hands dirty, I will put the source code and within the add comments to each method this for reading is easier

JSON
 




xxxxxxxxxx
1
516


 
1
/**
2
 
          
3
     *Variable Declarations
4
 
          
5
     * Buttons, TextViews
6
 
          
7
     * AuthHuaweiId to store retrived elements
8
 
          
9
     * ImageView to show avatar
10
 
          
11
     * Handler to use a service
12
 
          
13
    */
14
 
          
15
    private Button loginButton;
16
 
          
17
    private Button infoButton;
18
 
          
19
    private TextView welcomeTv,idtv,leveltv;
20
 
          
21
    private AuthHuaweiId mAuthid;
22
 
          
23
    private final static int SIGN_IN_INTENT = 3000;
24
 
          
25
    private String playerId;
26
 
          
27
    private String sessionId = null;
28
 
          
29
    private ImageView avatar;
30
 
          
31
    private Handler handler;
32
 
          
33
    private final static int HEARTBEAT_TIME = 15 * 60 * 1000;
34
 
          
35
    private static final String TAG = "TAG1";
36
 
          
37
    private boolean hasInit = false;
38
 
          
39
    
40
 
          
41
    //---------------------------------------------------------------------------------------------
42
 
          
43
    
44
 
          
45
    @Override
46
 
          
47
    protected void onCreate(Bundle savedInstanceState) {
48
 
          
49
        super.onCreate(savedInstanceState);
50
 
          
51
        setContentView(R.layout.activity_main);
52
 
          
53
        //Call UI elements we can use Butterknife or if you are using Kotlin use the extensions to get rid of this part
54
 
          
55
        loginButton = findViewById(R.id.loginButton);
56
 
          
57
        loginButton.setOnClickListener(this);
58
 
          
59
        welcomeTv = findViewById(R.id.textView);
60
 
          
61
        avatar = findViewById(R.id.avatarImg);
62
 
          
63
        infoButton = findViewById(R.id.btnInfo);
64
 
          
65
        infoButton.setOnClickListener(this);
66
 
          
67
        idtv = findViewById(R.id.idtv);
68
 
          
69
        leveltv = findViewById(R.id.leveltv);
70
 
          
71
        
72
 
          
73
    }
74
 
          
75
    //---------------------------------------------------------------------------------------------
76
 
          
77
    /*
78
 
          
79
    * Overide onclick method and by using elements id call the proper methods, dont forget to
80
 
          
81
    * implement View.Onclick Listener Interface
82
 
          
83
    */
84
 
          
85
    //---------------------------------------------------------------------------------------------
86
 
          
87
    @Override
88
 
          
89
    public void onClick(View view) {
90
 
          
91
        switch (view.getId()){
92
 
          
93
            case R.id.loginButton:
94
 
          
95
                signIn();
96
 
          
97
                break;
98
 
          
99
            case R.id.btnInfo:
100
 
          
101
                init();
102
 
          
103
                getCurrentPlayer();
104
 
          
105
                break;
106
 
          
107
  
108
 
          
109
        }
110
 
          
111
    }
112
 
          
113
  
114
 
          
115
    //---------------------------------------------------------------------------------------------
116
 
          
117
    /*
118
 
          
119
     * This is the method where we will initialize the service by passing the Authentification ID
120
 
          
121
     * if the user has logged in correctly
122
 
          
123
     */
124
 
          
125
    //---------------------------------------------------------------------------------------------
126
 
          
127
    public void init() {
128
 
          
129
        JosAppsClient appsClient = JosApps.getJosAppsClient(this,mAuthid);
130
 
          
131
        appsClient.init();
132
 
          
133
        Log.d(TAG,"init success");
134
 
          
135
        hasInit = true;
136
 
          
137
    }
138
 
          
139
    //---------------------------------------------------------------------------------------------
140
 
          
141
    /*
142
 
          
143
    *Get the current player by using the ID there are many ways to achive this but for this example
144
 
          
145
    * we will keep it simple
146
 
          
147
    **/
148
 
          
149
    //---------------------------------------------------------------------------------------------
150
 
          
151
    private void getCurrentPlayer() {
152
 
          
153
        PlayersClientImpl client = (PlayersClientImpl) Games.getPlayersClient(this,mAuthid);
154
 
          
155
        //Create a Task to get the Player
156
 
          
157
        Task<Player> task = client.getCurrentPlayer();
158
 
          
159
        task.addOnSuccessListener(new OnSuccessListener<Player>() {
160
 
          
161
            @Override
162
 
          
163
            public void onSuccess(Player player) {
164
 
          
165
                
166
 
          
167
                String result = "display:" + player.getDisplayName() + "\n" + "playerId:" + player.getPlayerId() + "\n"
168
 
          
169
                        + "playerLevel:" + player.getLevel() + "\n" + "timestamp:" + player.getSignTs() + "\n"
170
 
          
171
                        + "playerSign:" + player.getPlayerSign();
172
 
          
173
                Log.d("TAG1",result);
174
 
          
175
                idtv.setText(player.getPlayerId());
176
 
          
177
                leveltv.setText(player.getLevel() + "");
178
 
          
179
                playerId = player.getPlayerId();
180
 
          
181
                gameBegin();
182
 
          
183
                handler = new Handler() {
184
 
          
185
                    @Override
186
 
          
187
                    public void handleMessage(Message msg) {
188
 
          
189
                        super.handleMessage(msg);
190
 
          
191
                        //gamePlayExtra();
192
 
          
193
                    }
194
 
          
195
                };
196
 
          
197
                new Timer().schedule(new TimerTask() {
198
 
          
199
                    @Override
200
 
          
201
                    public void run() {
202
 
          
203
                        Message message = new Message();
204
 
          
205
                        handler.sendMessage(message);
206
 
          
207
                    }
208
 
          
209
                }, HEARTBEAT_TIME, HEARTBEAT_TIME);
210
 
          
211
            }
212
 
          
213
        }).addOnFailureListener(new OnFailureListener() {
214
 
          
215
            @Override
216
 
          
217
            public void onFailure(Exception e) {
218
 
          
219
                if (e instanceof ApiException) {
220
 
          
221
                    String result = "rtnCode:" + ((ApiException) e).getStatusCode();
222
 
          
223
                    Log.d("TAG1",result);
224
 
          
225
                }
226
 
          
227
            }
228
 
          
229
        });
230
 
          
231
    }
232
 
          
233
    //---------------------------------------------------------------------------------------------
234
 
          
235
    /*
236
 
          
237
    *On this method the Game will begin with the obtained ID
238
 
          
239
    */
240
 
          
241
    //---------------------------------------------------------------------------------------------
242
 
          
243
    public void gameBegin() {
244
 
          
245
        if (TextUtils.isEmpty(playerId)) {
246
 
          
247
            Log.d("TAG1","GetCurrentPlayer first.");
248
 
          
249
            return;
250
 
          
251
        }
252
 
          
253
        String uid = UUID.randomUUID().toString();
254
 
          
255
        PlayersClient client = Games.getPlayersClient(this,mAuthid);
256
 
          
257
        Task<String> task = client.submitPlayerEvent(playerId, uid, "GAMEBEGIN");
258
 
          
259
        task.addOnSuccessListener(new OnSuccessListener<String>() {
260
 
          
261
            @Override
262
 
          
263
            public void onSuccess(String jsonRequest) {
264
 
          
265
                if (jsonRequest == null) {
266
 
          
267
                    Log.d("TAG1","jsonRequest is null");
268
 
          
269
                    return;
270
 
          
271
                }
272
 
          
273
                try {
274
 
          
275
                    JSONObject data = new JSONObject(jsonRequest);
276
 
          
277
                    sessionId = data.getString("transactionId");
278
 
          
279
                } catch (JSONException e) {
280
 
          
281
                    Log.d("TAG1","parse jsonArray meet json exception");
282
 
          
283
                    return;
284
 
          
285
                }
286
 
          
287
                Log.d("TAG1","submitPlayerEvent traceId: " + jsonRequest);
288
 
          
289
            }
290
 
          
291
        }).addOnFailureListener(new OnFailureListener() {
292
 
          
293
            @Override
294
 
          
295
            public void onFailure(Exception e) {
296
 
          
297
                if (e instanceof ApiException) {
298
 
          
299
                    String result = "rtnCode:" + ((ApiException) e).getStatusCode();
300
 
          
301
                    Log.d("TAG1",result);
302
 
          
303
                }
304
 
          
305
            }
306
 
          
307
        });
308
 
          
309
    }
310
 
          
311
    //---------------------------------------------------------------------------------------------
312
 
          
313
    /*
314
 
          
315
    *   This is the method to sign in i have commented most of the lines to understa what we have been doing
316
 
          
317
    */
318
 
          
319
    //---------------------------------------------------------------------------------------------
320
 
          
321
    private void signIn() {
322
 
          
323
        //Create a task with the Type of AuthHuaweiId
324
 
          
325
        Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.getService(this, getHuaweiIdParams()).silentSignIn();
326
 
          
327
        //Add the proper Listener so we can track the response of the Task
328
 
          
329
        authHuaweiIdTask.addOnSuccessListener(new OnSuccessListener<AuthHuaweiId>() {
330
 
          
331
            //Must overide the Osuccess Method which will return an AuthHuaweiId Object
332
 
          
333
            @Override
334
 
          
335
            public void onSuccess(AuthHuaweiId authHuaweiId) {
336
 
          
337
                //Logs to track the information
338
 
          
339
                Log.d("TAG1","signIn success");
340
 
          
341
                Log.d("TAG1","Id" + authHuaweiId.getDisplayName());
342
 
          
343
                Log.d("TAG1", "Picture" + authHuaweiId.getAvatarUriString());
344
 
          
345
                //Handle the user interface
346
 
          
347
                welcomeTv.setVisibility(View.VISIBLE);
348
 
          
349
                welcomeTv.setText("Welcome back " + authHuaweiId.getDisplayName());
350
 
          
351
                Picasso.get().load(authHuaweiId.getAvatarUriString()).into(avatar);
352
 
          
353
  
354
 
          
355
                loginButton.setVisibility(View.INVISIBLE);
356
 
          
357
                Log.d("TAG1","AT:" + authHuaweiId.getAccessToken());
358
 
          
359
                
360
 
          
361
                mAuthid = authHuaweiId;
362
 
          
363
                //Sign in Center update
364
 
          
365
                SignInCenter.get().updateAuthHuaweiId(authHuaweiId);
366
 
          
367
                infoButton.setVisibility(View.VISIBLE);
368
 
          
369
  
370
 
          
371
            }
372
 
          
373
        }).addOnFailureListener(new OnFailureListener() {
374
 
          
375
            //Something went wrong, use this method to inform your users
376
 
          
377
            @Override
378
 
          
379
            public void onFailure(Exception e) {
380
 
          
381
                if (e instanceof ApiException) {
382
 
          
383
                    ApiException apiException = (ApiException) e;
384
 
          
385
                    Log.d("TAG1","signIn failed:" + apiException.getStatusCode());
386
 
          
387
                    Log.d("TAG1","start getSignInIntent");
388
 
          
389
                    signInNewWay();
390
 
          
391
                }
392
 
          
393
            }
394
 
          
395
        });
396
 
          
397
  
398
 
          
399
    }
400
 
          
401
    //---------------------------------------------------------------------------------------------
402
 
          
403
    private void signInNewWay() {
404
 
          
405
        Intent intent = HuaweiIdAuthManager.getService(MainActivity.this, getHuaweiIdParams()).getSignInIntent();
406
 
          
407
        startActivityForResult(intent, SIGN_IN_INTENT);
408
 
          
409
    }
410
 
          
411
    //---------------------------------------------------------------------------------------------
412
 
          
413
    /*
414
 
          
415
    *Create the HuaweiIdParams so we can send it to the service
416
 
          
417
    */
418
 
          
419
    //---------------------------------------------------------------------------------------------
420
 
          
421
    public HuaweiIdAuthParams getHuaweiIdParams() {
422
 
          
423
        return new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM_GAME).createParams();
424
 
          
425
    }
426
 
          
427
    //---------------------------------------------------------------------------------------------
428
 
          
429
    /*
430
 
          
431
    *Dont Forget to overide onActivity Result otherwise we wont have any functionality working
432
 
          
433
    * */
434
 
          
435
    //---------------------------------------------------------------------------------------------
436
 
          
437
    @Override
438
 
          
439
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
440
 
          
441
        super.onActivityResult(requestCode, resultCode, data);
442
 
          
443
        if (SIGN_IN_INTENT == requestCode) {
444
 
          
445
            
446
 
          
447
            handleSignInResult(data);
448
 
          
449
        } else {
450
 
          
451
            Log.d("TAG1","unknown requestCode in onActivityResult");
452
 
          
453
        }
454
 
          
455
    }
456
 
          
457
    //---------------------------------------------------------------------------------------------
458
 
          
459
    /*
460
 
          
461
    *Handle the result of signin we can take some desicion here
462
 
          
463
    * */
464
 
          
465
    //---------------------------------------------------------------------------------------------
466
 
          
467
    private void handleSignInResult(Intent data) {
468
 
          
469
        if (null == data) {
470
 
          
471
            Log.d("TAG1","signIn inetnt is null");
472
 
          
473
            return;
474
 
          
475
        }
476
 
          
477
        // HuaweiIdSignIn.getSignedInAccountFromIntent(data);
478
 
          
479
        String jsonSignInResult = data.getStringExtra("HUAWEIID_SIGNIN_RESULT");
480
 
          
481
        if (TextUtils.isEmpty(jsonSignInResult)) {
482
 
          
483
            Log.d("TAG1","SignIn result is empty");
484
 
          
485
            return;
486
 
          
487
        }
488
 
          
489
        try {
490
 
          
491
            HuaweiIdAuthResult signInResult = new HuaweiIdAuthResult().fromJson(jsonSignInResult);
492
 
          
493
            if (0 == signInResult.getStatus().getStatusCode()) {
494
 
          
495
                Log.d("TAG1","Sign in success.");
496
 
          
497
                Log.d("TAG1","Sign in result: " + signInResult.toJson());
498
 
          
499
                SignInCenter.get().updateAuthHuaweiId(signInResult.getHuaweiId());
500
 
          
501
               // getCurrentPlayer();
502
 
          
503
            } else {
504
 
          
505
                Log.d("TAG1","Sign in failed: " + signInResult.getStatus().getStatusCode());
506
 
          
507
            }
508
 
          
509
        } catch (JSONException var7) {
510
 
          
511
            Log.d("TAG1","Failed to convert json from signInResult.");
512
 
          
513
        }
514
 
          
515
    }



Test the App

Well because that step was quite long, the important thing is to understand what is done within the code for future implementations. Well, now let's check the operation of the App. We should have the following


A screen with the information that will be modified when logging in and bringing the information.

Actions

Click on the Login button.- The user's session will start and the Welcome title will be modified by adding the user's name


We hide the Login button and modify the Title.

Actions

-Click on the user information button will call the service and we will obtain the user's ID and level information


Our App has managed to bring the avatar, the id of the player, and the level of the player. This user needs to download some games and raise their level as he is still a novice.

Conclusion

Well! Well we have achieved it we have a small implementation of a simple Game Service but that we could add to our video games. Game Service has many very useful methods, check the documentation if you would like to go deeper into this topic.

https://developer.huawei.com/consumer/en/hms/huawei-game

Topics:
android, application, integration, mobile, opensource

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}