目录

The AchievementsClient class defines APIs for managing game achievements, for example, obtaining the game achievement list, incrementing an achievement, and setting steps required for unlocking an achievement.

Public Method Summary

ReturnMethod
Task<Intent>getShowAchievementListIntent()
Obtains the list of all game achievements of the current player.
voidgrow(String id, int numSteps)
Asynchronously increases an achievement by the given number of steps.
Task<Boolean>growWithResult(String id, int numSteps)
Synchronously increases an achievement by the given number of steps.
Task<List<Achievement>>getAchievementList(boolean forceReload)
Obtains the list of achievements of the current player.
voidvisualize(String id)
Asynchronously reveals a hidden achievement to the current player.
Task<Void>visualizeWithResult(String id)
Synchronously reveals a hidden achievement to the current player.
voidmakeSteps(String id, int numSteps)
Asynchronously sets an achievement to have the given number of steps completed.
Task<Boolean>makeStepsWithResult(String id, int numSteps)
Synchronously sets an achievement to have the given number of steps completed.
voidreach(String id)
Asynchronously unlocks an achievement for the current player.
Task<Void>reachWithResult(String id)
Synchronously unlocks an achievement for the current player.

Public Methods

public Task< Intent> getShowAchievementListIntent()

Obtains the list of all game achievements of the current player. This method is called only when a player views the achievement list.

Return Values

Task< Intent>Intent object containing the achievement list. An app can call the startActivityForResult method to access the achievement list.

You need to set two callback listeners in the Task returned by the API. OnSuccessListener obtains the data returned in the success scenario, and OnFailureListener obtains the information about the request failure. In OnFailureListener, you need to check whether the exception object called back in this mode is an ApiException instance object, and resolve the failure according to getStatusCode() contained in the ApiException instance. For details about the return code, see Game API Result Codes.

Example

  1. private final static int REQUEST_CODE = 3000;    
  2. client.getShowAchievementListIntent().addOnSuccessListener(new OnSuccessListener<Intent>() {    
  3.     @Override    
  4.    public void onSuccess(Intent intent) {    
  5.         startActivityForResult(intent, REQUEST_CODE);    
  6.     }    
  7. });    

public void grow(String id, int numSteps)

Asynchronously increases an achievement by the given number of steps. For example, if 5 steps are required to complete an achievement, this method needs to be called to increase the number of completed steps by one when the player completes step 2 and goes to step 3. The achievement must be an achievement that requires multiple steps and has not been unlocked. Once the player completes all steps of an achievement, the achievement is automatically unlocked and any further request to call this method will be ignored.
This method is submitted asynchronously. To be specific, if the device is not connected to the network when the app client calls this method, the HMS SDK caches the data locally. The data is submitted to the Huawei server when network connection is restored and the user logs in again. It is unnecessary for developers to pay attention to the execution result of this method.
To immediately increase an achievement by the given number of steps or obtain the method execution result, use the growWithResult method.

Parameters

Parameter NameParameter Description
idID of the achievement requiring step increase.
numStepsNumber of steps to be increased. The value must be greater than 0.

public Task<Boolean> growWithResult(String id, int numSteps)

Immediately increases an achievement by the given number of steps. For example, if 5 steps are required to complete an achievement, this method needs to be called to increase the number of completed steps by one when the player completes step 2 and goes to step 3. The achievement must be an achievement that requires multiple steps and has not been unlocked. Once the player completes all steps of an achievement, the achievement is automatically unlocked and any further request to call this method will be ignored.
This method submits requests synchronously. That is, requests are immediately sent to the Huawei server after an app client calls this method, and the execution result is returned immediately.

Parameters

Parameter NameParameter Description
idID of the achievement requiring step increase.
numStepsNumber of steps to be increased. The value must be greater than 0.

Return Values

Task< Boolean>Execution result. The value true indicates that the execution is successful and the value false indicates that the API is called successfully but fails to be executed. If the execution fails, you need to check whether the achievement ID and set number of steps are correct.

You need to set two callback listeners in the Task returned by the API.
OnSuccessListener obtains the data returned in the success scenario, and OnFailureListener obtains the information about the request failure. In OnFailureListener, you need to check whether the exception object called back in this mode is an ApiException instance object, and resolve the failure according to getStatusCode() contained in the ApiException instance. For details about the return code, see Game API Result Codes.
If the number of steps reaches the maximum after step increase, HMS SDK will return the 7202 result code, indicating successful step increase, and the achievement will be automatically unlocked.

Example

  1. Task< Boolean> task = client.growWithResult(achievementId, 1);    
  2. task.addOnSuccessListener(new OnSuccessListener<Boolean>() {    
  3.    @Override    
  4.    public void onSuccess(Boolean isSuccess) {    
  5.        }    
  6.    }).addOnFailureListener(new OnFailureListener() {    
  7.    @Override    
  8.    public void onFailure(Exception e) {    
  9.        if (e instanceof ApiException) {    
  10.            int rtnCode = ((ApiException) e).getStatusCode();    
  11.        }    
  12.    }    
  13. });    

public Task<List<Achievement>> getAchievementList(boolean forceReload)

Obtains the list of achievements of the current player.

Parameters

Parameter NameParameter Description
forceReloadIndicates to obtain the achievement list from the server or client. The options are as follows:
● true: server
● false: client
            

Return Values

Task<List<Achievement>>The list of achievements of the current player.

You need to set two callback listeners in the Task returned by the API.
OnSuccessListener obtains the data returned in the success scenario, and OnFailureListener obtains the information about the request failure. In OnFailureListener, you need to check whether the exception object called back in this mode is an ApiException instance object, and resolve the failure according to getStatusCode() contained in the ApiException instance. For details about the return code, see Game API Result Codes.

public void visualize(String id)

Asynchronously reveals a hidden achievement of a game. This method needs to be called only when the player triggers the preset scene of the achievement. If the achievement has been unlocked for the current player, this method does not work.
This method is submitted asynchronously. To be specific, if the device is not connected to the network when the app client calls this method, the HMS SDK caches the data locally. The data is submitted to the Huawei server when network connection is restored and the user logs in again. It is unnecessary for developers to pay attention to the execution result of this method.
To immediately reveal an achievement or obtain the method execution result, use the visualizeWithResult method.

Parameters

Parameter NameParameter Description
idID of the achievement to be revealed.

public Task<Void> visualizeWithResult(String id)

Immediately reveals a hidden achievement of a game. This method needs to be called only when the player triggers the preset scene of the achievement. If the achievement has been unlocked for the current player, this method does not work.
This method submits requests synchronously. That is, requests are immediately sent to the Huawei server after an app client calls this method, and the execution result is returned immediately.

Parameters

Parameter NameParameter Description
idID of the achievement to be revealed.

Return Values
None

You need to set two callback listeners in the Task returned by the API.
OnSuccessListener obtains the data returned in the success scenario, and OnFailureListener obtains the information about the request failure. In OnFailureListener, you need to check whether the exception object called back in this mode is an ApiException instance object, and resolve the failure according to getStatusCode() contained in the ApiException instance. For details about the return code, see Game API Result Codes.

Example

  1. Task<Void> task = client.visualizeWithResult(achievementId);    
  2. task.addOnSuccessListener(new OnSuccessListener<Void>() {    
  3.    @Override    
  4.    public void onSuccess(Void v) {    
  5.        }    
  6.    }).addOnFailureListener(new OnFailureListener() {    
  7.    @Override    
  8.    public void onFailure(Exception e) {    
  9.        if (e instanceof ApiException) {    
  10.            int rtnCode = ((ApiException) e).getStatusCode();    
  11.        }    
  12.    }    
  13. });    

public void makeSteps(String id, int numSteps)

Asynchronously sets the number of completed steps of an achievement. For example, if 5 steps are required to complete an achievement, this method needs to be called to set the number of completed steps to 3 when the player completes step 1 to step 3. Once the player completes all steps of an achievement, the achievement is automatically unlocked and any further request to call this method will be ignored.
This method is submitted asynchronously. To be specific, if the device is not connected to the network when the app client calls this method, the HMS SDK caches the data locally. The data is submitted to the Huawei server when network connection is restored and the user logs in again. It is unnecessary for developers to pay attention to the execution result of this method.
To immediately set the number of completed steps of an achievement or obtain the method execution result, use the makeStepsWithResult method.

Parameters

Parameter NameParameter Description
idID of the achievement requiring step setup.
numStepsNumber of steps to be set. The value must be greater than 0.

public Task<Boolean> makeStepsWithResult(String id, int numSteps)

Immediately sets the number of completed steps of an achievement. For example, if 5 steps are required to complete an achievement, this method needs to be called to set the number of completed steps to 3 when the player completes step 1 to step 3. Once the player completes all steps of an achievement, the achievement is automatically unlocked and any further request to call this method will be ignored.
This method submits requests synchronously. That is, requests are immediately sent to the Huawei server after an app client calls this method, and the execution result is returned immediately.

Parameters

Parameter NameParameter Description
idID of the achievement requiring step setup.
numStepsNumber of steps to be set. The value must be greater than 0.

Return Values

Task< Boolean>Execution result. The value true indicates that the execution is successful and the value false indicates that the API is called successfully but fails to be executed. If the execution fails, you need to check whether the achievement ID and set number of steps are correct.

You need to set two callback listeners in the Task returned by the API.
OnSuccessListener obtains the data returned in the success scenario, and OnFailureListener obtains the information about the request failure. In OnFailureListener, you need to check whether the exception object called back in this mode is an ApiException instance object, and resolve the failure according to getStatusCode() contained in the ApiException instance. For details about the return code, see Game API Result Codes.
If the number of steps reaches the maximum after step setup, HMS SDK will return the 7202 result code, indicating successful step setup, and the achievement will be automatically unlocked.

Example

  1. Task<Boolean> task = client.makeStepsWithResult(achievementId,stepsNum);    
  2. task.addOnSuccessListener(new OnSuccessListener<Boolean>() {    
  3.    @Override    
  4.    public void onSuccess(Boolean isSucess) {    
  5.    }    
  6.    }).addOnFailureListener(new OnFailureListener() {    
  7.    @Override    
  8.    public void onFailure(Exception e) {    
  9.        int rtnCode = ((ApiException) e).getStatusCode();    
  10.    }    
  11. });    

public void reach(String id)

Asynchronously unlocks an achievement for the current player. This method needs to be called only when the player has fulfilled the requirements specified for an achievement. If the achievement is a hidden one, this method will reveal and unlock the achievement.
This method is submitted asynchronously. To be specific, if the device is not connected to the network when the app client calls this method, the HMS SDK caches the data locally. The data is submitted to the Huawei server when network connection is restored and the user logs in again. It is unnecessary for developers to pay attention to the execution result of this method.
To immediately set the number of steps required for unlocking an achievement or obtain the method execution result, use the reachWithResult method.

Parameters

Parameter NameParameter Description
idID of the achievement to be unlocked.

public Task<Void> reachWithResult(String id)

Immediately unlocks an achievement for the current player. This method needs to be called only when the player has fulfilled the requirements specified for an achievement.
This method submits requests synchronously. That is, requests are immediately sent to the Huawei server after an app client calls this method, and the execution result is returned immediately.

Parameters

Parameter NameParameter Description
idID of the achievement to be unlocked.

Return Values
None

You need to set two callback listeners in the Task returned by the API.
OnSuccessListener obtains the data returned in the success scenario, and OnFailureListener obtains the information about the request failure. In OnFailureListener, you need to check whether the exception object called back in this mode is an ApiException instance object, and resolve the failure according to getStatusCode() contained in the ApiException instance. For details about the return code, see Game API Result Codes.

Example

  1. Task<Void> task = client.reachWithResult(achievementId);    
  2. task.addOnSuccessListener(new OnSuccessListener<Void>() {    
  3.    @Override    
  4.    public void onSuccess(Void v) {    
  5.        }    
  6.    }).addOnFailureListener(new OnFailureListener() {    
  7.    @Override    
  8.    public void onFailure(Exception e) {    
  9.        if (e instanceof ApiException) {    
  10.            int rtnCode = ((ApiException) e).getStatusCode();    
  11.        }    
  12.    }    
  13. });