GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(getActivity());
gamesSignInClient.isAuthenticated().addOnCompleteListener(isAuthenticatedTask -> {
boolean isAuthenticated =
(isAuthenticatedTask.isSuccessful() &&
isAuthenticatedTask.getResult().isAuthenticated());
if (isAuthenticated) {
// Continue with Play Games Services
} else {
// Show a sign-in button to ask players to authenticate. Clicking it should
// call GamesSignInClient.signIn().
}
});
防止自动触发的个人资料创建
您可以通过清单文件停用自动触发的个人资料创建提示。这样,没有 Play 游戏服务个人资料的用户便可以继续加载游戏,而不会收到创建 Play 游戏服务个人资料的提示。
如需了解详情,请参阅个人资料创建选项。
如需使用此功能,请确保满足以下条件:
在设备上登录的任何 Google 账号都没有 Play 游戏服务个人资料。
您的游戏已与 Play 游戏服务 SDK com.google.android.gms:play-services-games-v2:21.0.0 或更高版本集成。
这样,没有 Play 游戏服务个人资料的用户便可以继续使用您实现的身份验证方法,而不会立即收到创建 Play 游戏服务个人资料的提示。
您可以通过调用
GamesSignInService.signin()来启动个人资料创建。
importcom.google.android.gms.games.PlayGames;...// Get the achievements client using Play Games services.AchievementsClientachievementsClient=PlayGames.getAchievementsClient(getActivity());achievementsClient.getAchievementsIntent().addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptionexception){intstatusCode=((ApiException)exception).getStatusCode();if(statusCode==GamesClientStatusCodes.SIGN_IN_REQUIRED){// SIGN_IN_REQUIRED: The user needs to sign in with Play Games Services.// Call GamesSignInService.signin() to prompt for// authentication at a suitable time which will trigger the// profile creation UI.// (e.g., after a tutorial). Use GamesSignInService.isAuthenticated() to check auth status.}}});
添加抑制标记后,请使用 logcat 窗口验证添加操作。logcat 输出包含类似于以下内容的消息:“Game opted out of automatic profile creation prompt (using manifest)”。