【问题标题】:Pop-up not be displayed when receiving an achievement using Google Play Games使用 Google Play 游戏接收成就时不显示弹出窗口
【发布时间】:2019-01-04 11:59:09
【问题描述】:

我发现了一堆有类似问题的帖子,但大多数都使用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,如果我确实需要你能告诉我为什么以及如何使用吗? 我们将不胜感激所有帮助!

【问题讨论】:

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


    【解决方案1】:

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

    我从this StackOverflow answer 将以下内容添加到我的OnConnected

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

    它给出了 GoogleSignIn.getLastSignedInAccount(this)) 可能为空的 lint 警告,所以我只是将它包装在 if statement 列表中:

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

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

    【讨论】:

    • 我试过这个,还在我的主布局中添加了那个视图块gps_popup。我没有收到任何错误,但也没有看到任何弹出窗口。
    • @MattVine 我将代码从OnCreate 移至OnConnected,它运行得更好。我尝试在登录过程中将其移动到不同的位置,并通过登录和退出应用程序进行故障排除,直到它按预期运行并且 Google Play 游戏用户名会浮在视图上并消失。
    • 我找不到OnConnected。在我的代码中将OnConnected 放在哪里?
    • @MattVine 我相信我们使用不同的技术连接到 Google Play 游戏服务。我遵循了在 Github 上托管的 Google TypeANumber 教程,其中包括一个 OnConnected 函数。您还可以在 Github here 上查看我在遵循 Google 教程时创建的实现此修复的项目。
    【解决方案2】:

    如果您正在测试游戏服务并且没有显示成就的弹出窗口,原因可能是您没有为该成就添加图标。我发现,弹出窗口仅针对 ICON 的成就显示

    【讨论】:

    • 我已经仔细检查了,我所有的成就在开发者控制台中都有图标。
    • @MattVine 你试过清除 Play Games Service 应用的缓存吗?
    猜你喜欢
    • 2015-03-01
    • 2014-12-15
    • 2016-07-14
    • 2018-05-12
    • 2017-09-04
    • 2014-01-11
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多