android - 使用 Google Play 游戏获得成就时不会显示弹出窗口

标签 android google-play-games google-api-client

我发现了一堆有类似问题的帖子,但大多数都在使用我没有的 BaseGameUtilsGoogleApiClient。 一个建议的答案是添加 Games.setViewForPopups(getApiClient(), findViewById(R.id.gps_popup)); 除非我有 GoogleApiClient,否则我不能这样做。

成就代码:

public void achieve(String achievement) {
    Games.getAchievementsClient(this, signedInAccount).unlock(achievement);
}

public void increment(String achievement, int i) {
    Games.getAchievementsClient(this, signedInAccount).increment(achievement, i);
}

登录码:

@Override
protected void onCreate(Bundle _savedInstanceState) {
    ...
    signInSilently();
}

private void signInSilently() {
    GoogleSignInClient signInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
    signInClient.silentSignIn().addOnCompleteListener(this,
            new OnCompleteListener<GoogleSignInAccount>() {
                @Override
                public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                    if (task.isSuccessful()) {
                        signedInAccount = task.getResult();
                        findViewById(R.id.achievements_btn).setVisibility(View.VISIBLE);
                    } else {
                        startSignInIntent();
                    }
                }
            });
}

private void startSignInIntent() {
    GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
            GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
    Intent intent = signInClient.getSignInIntent();
    startActivityForResult(intent, RC_SIGN_IN);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            signedInAccount = result.getSignInAccount();
            findViewById(R.id.achievements_btn).setVisibility(View.VISIBLE);
            Toast.makeText(getApplicationContext(), "Signed In", Toast.LENGTH_SHORT).show();
        } else {
            String message = result.getStatus().getStatusMessage();
            if (message == null || message.isEmpty()) {
                message = "Error";
            }
            Toast.makeText(getApplicationContext(), "Sign In Failed, please check internet connection and restart the app to use achievements.", Toast.LENGTH_LONG).show();
            cancelledSignIn = true;
        }
    }
}


private void showAchievements() {
    Games.getAchievementsClient(this, signedInAccount)
            .getAchievementsIntent()
            .addOnSuccessListener(new OnSuccessListener<Intent>() {
                @Override
                public void onSuccess(Intent intent) {
                    startActivityForResult(intent, RC_ACHIEVEMENT_UI);
                }
            });
}

不知道该怎么做,除了弹出窗口从不显示外,这一切都运行良好。我从来不需要使用 GoogleApiClient,如果我确实需要,您能告诉我为什么以及如何使用吗? 将不胜感激所有帮助!

最佳答案

我有同样的问题,也没有使用 BaseGameUtilsGoogleApiClient

我将以下内容添加到我的 OnConnected 来自 this StackOverflow answer .

GamesClient gamesClient = Games.getGamesClient(MainActivity.this, 
GoogleSignIn.getLastSignedInAccount(context));
gamesClient.setViewForPopups(findViewById(R.id.gps_popup));

它给出了 GoogleSignIn.getLastSignedInAccount(this)) 可能为 null 的 lint 警告,所以我只是将其包装在 if 语句 中,列出以下内容:

if(GoogleSignIn.getLastSignedInAccount(this) != null){
GamesClient gamesClient = Games.getGamesClient(MainActivity.this, 
GoogleSignIn.getLastSignedInAccount(context));
gamesClient.setViewForPopups(findViewById(R.id.gps_popup));
}

这可能不是一个很好的答案,但它对我有用,所以我希望你能发现它有用,祝你好运。

关于android - 使用 Google Play 游戏获得成就时不会显示弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51556616/

相关文章:

Android sleep() 不阻塞 UI

java - putExtras(child class) - 接收基类

android - Android Market 搜索引擎是如何工作的?

android - Geocoder.getFromLocation() 不返回任何地址

android - 是否可以查询Google Play服务器以了解用户是否购买了某件商品?

android - Google Game Play Service Achievement 不弹出

google-api - 如何重置 google oauth 2.0 授权?

android - Auth.GOOGLE_SIGN_IN_API 不能与 Games.API 一起使用

android - 为什么 Google Play 游戏图标未显示在 Play 商店中?

google-api - 谷歌 API : object oauth2 is not a member of package com. google.api.services