跳到主要内容

Google Play Games登录

前置条件

Google Play Console 中创建一个凭证,并选择你在 Google Cloud Console 中创建的 OAuth 客户端。

google_play_credential.jpg

然后获取应用 ID:

google_play_credential2.jpg

添加依赖

在应用级别的 build.gradle 中添加以下代码,并将 $msdk_version 替换为实际的 MSDK 版本。

dependencies {
implementation "com.garena.sdk.android:login-pgs:$msdk_version"
}

配置

配置 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

...

<application ...>

<meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="[google_play_games_app_id]" />

<meta-data
android:name="com.garena.sdk.gms.games.OAUTH_CLIENT_ID"
android:value="[google_play_games_oauth_client_id]" />

....
</application>

</manifest>

使用方法

AccountManager accountManager = new AccountManager(activity);
accountManager.login(PlatformType.GOOGLE_PLAY_GAMES, onLoginListener);

Google Play Games Recall

前置条件

  1. 在 Google Console 中设置 Google Play Games 测试账号。
  2. 如果你已经引入了 Google Play Games 依赖,需要将该依赖移除。
// remove this line
implementation "com.google.android.gms:play-services-games-v2:17.0.0"

使用方法

在登录成功(包括自动登录成功)后,应调用 saveToken,以保证 Google Play Games 的 token 是最新的。

PlayGamesRecallManager recallManager = new PlayGamesRecallManager(activity);
recallManager.saveToken(result -> {...});

异步检查 Google Play Games 的登录状态。

recallManager.checkSignInState(result -> {
...
});

回溯 token 并恢复登录。

recallManager.retrieveToken(result -> {
if (result.isSuccess()) {
// Currently server only returns the last RecallToken.
// But in the future we may return multiple tokens, that's why here's a list
List<RecallToken> tokens = result.unwrap();
if (!tokens.isEmpty()) {
RecallToken recallToken = tokens.get(0);
recallManager.restoreLogin(recallToken, callback);
}
}
});