You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

集成Google Play Games登录时getCurrentPlayer()回调未执行的问题求助

Why isn't OnSuccessListener for PlayersClient.getCurrentPlayer() executing?

Let’s dive straight into the root issue here—your code has a critical blocking problem that’s preventing the callback from ever running:

The main culprit: You’re locking the UI thread with an infinite loop

Look at this section of your code:

while(playerName==null){
    Log.d("OH GOD WHY", "WHY???");
}
//Do something with the name ...

PlayersClient.getCurrentPlayer() is an asynchronous operation—it runs in the background, and its OnSuccessListener is meant to execute on the main (UI) thread once it finishes. But you’ve trapped the main thread in an infinite while loop waiting for playerName to update. Since the main thread can’t process any other tasks while this loop runs, the callback never gets a chance to fire. That’s why playerName stays null forever!

Fix: Embrace asynchronous flow—move your logic into the callback

You need to refactor your code to work with, not against, the async pattern. Here’s the corrected version:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SIGN_IN) {
        Task<GoogleSignInAccount> resTask = GoogleSignIn.getSignedInAccountFromIntent(data);
        if (resTask.isSuccessful()) {
            GoogleSignInAccount resAcct = resTask.getResult();
            PlayersClient playersClient = Games.getPlayersClient(this, resAcct);
            playersClient.getCurrentPlayer()
                .addOnSuccessListener(taskPlayer -> {
                    playerName = taskPlayer.getName();
                    // DO EVERYTHING THAT NEEDS playerName RIGHT HERE
                    Log.d("SUCCESS", "Got player name: " + playerName);
                    // Example: Update UI elements, save to shared preferences, etc.
                })
                .addOnFailureListener(e -> {
                    // Handle failures gracefully here
                    Log.e("PLAYER_ERROR", "Failed to fetch player info", e);
                    Toast.makeText(getApplicationContext(), "Could not load player details", Toast.LENGTH_SHORT).show();
                });
            // Remove that infinite while loop completely!
        } else {
            Toast.makeText(getApplicationContext(), "Unable to sign in. Please try again later.", Toast.LENGTH_SHORT).show();
        }
    }
}

Additional checks (if the callback still doesn’t run after fixing the loop)

  • Confirm your app is properly linked to your Google Play Games project in the Google Play Console: You’ve configured the game entry, added the correct OAuth 2.0 client ID, and linked your app’s package name/signature.
  • Ensure the user has Google Play Games installed and signed in on their device.
  • Verify you’ve added required permissions in AndroidManifest.xml:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    
  • Make sure you’re using the latest version of the Google Play Games SDK in your build.gradle files.

This same blocking issue was why your OnCompleteListener attempt failed too—your infinite loop kept the main thread occupied, so onComplete never got executed. Fixing the loop will resolve both cases.

内容的提问来源于stack exchange,提问作者Kornel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:26:14