当调用Games.Achievements.unlock时,没有弹出窗口显示

13

我的当前Android游戏采用BaseGameActivity。

我的游戏使用成就系统,当达到要求时会解锁成就。

然而,并不总是能看到与解锁事件相关的弹出窗口。

我知道这个弹出窗口只会在你第一次解锁成就时出现。

有些弹出窗口可以正确显示,其他一些(来自我游戏内不同屏幕的)则从未出现过。

我应该怎么做才能保证弹出窗口出现呢?

我感觉这可能与这个警告有关:

W/PopupManager(10725): You have not specified a View to use as content view for popups.

Falling back to the Activity content view which may not work properly in future versions
of the API. 
Use setViewForPopups() to set your content view.

我已经从我的弹出窗口所在的活动中调用了 setViewForPopups() 方法,但是我从未看到过它们。

如何调用 setViewForPopups() 方法,以便您的整个应用程序都不会看到上面显示的警告消息?

6个回答

9
我找到了一个解决办法,使用以下代码:
Games.setViewForPopups(getApiClient(), getWindow().getDecorView().findViewById(android.R.id.content));

我可以让弹出窗口显示。现在我有一个相关的问题,就是弹出窗口没有显示太久。我想这是因为我在这个活动中做了自定义动画。有没有办法延长弹出窗口的可见时间?

你在哪里使用这段代码?我也收到了同样的警告,但是我在我的项目中只发现GPS库的Games类中使用了这个方法。我应该在BaseGameActivity中手动使用这段代码吗?谢谢。 - luben

6

最近的Play服务框架更新有一些变化。请使用以下内容,以便您可以看到问候弹出窗口和解锁弹出窗口。

GamesClient gamesClient = Games.getGamesClient(this, GoogleSignIn.getLastSignedInAccount(this));
gamesClient.setViewForPopups(findViewById(android.R.id.content));
gamesClient.setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL);

不要忘记像这样导入所需的内容 -
import android.view.Gravity;
import com.google.android.gms.games.GamesClient;

5
这对我有效。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate");

    setContentView(R.layout.activity_main);

    // Create the Google API Client with access to Plus, Games and Drive
    // Also set the view for popups
    mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER)
            .setViewForPopups(findViewById(android.R.id.content))
            .build();

}

android.R.id.content 给你一个视图的根元素,而不需要知道它的实际名称/类型/ID。请查看获取当前活动的根视图


5

Games.setViewForPopups已被弃用

要显示弹出窗口,请将以下代码添加到您的活动或片段布局中:

 <FrameLayout
    android:id="@+id/container_pop_up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="16dp" />

在初始化AchievementsClient对象的代码后面添加下面的代码

 GamesClient gamesClient = Games.getGamesClient(MainActivity.this, googleSignInAccount);
 gamesClient.setViewForPopups(findViewById(R.id.container_pop_up));

其中googleSignInAccount是GoogleSignInAccount对象


0

对于那些在 Kotlin 中遇到困难的人,这是我使用的方法:

private fun signInSilently() {
    mGoogleSignInClient.silentSignIn().addOnCompleteListener(this)
    { task ->
        if (task.isSuccessful) {
            Log.d(LOG_TAG, "signInSilently(): success")
            mAchievementsClient = Games.getAchievementsClient(this, task.result!!)
            val gamesClient = Games.getGamesClient(this@AchievementsScreen, GoogleSignIn.getLastSignedInAccount(this)!!)
            gamesClient.setViewForPopups(findViewById(android.R.id.content))
            gamesClient.setGravityForPopups(Gravity.TOP or Gravity.CENTER_HORIZONTAL)
        } else {
            Log.d(LOG_TAG, "signInSilently(): failure", task.exception)
            startSignInIntent()
        }
    }
}

你知道如果我们使用Jetpack Compose,如何设置setViewForPopups吗? - smbdy

0

这个(Kotlin)完美地运行了:

    Games.getGamesClient(this, googleSignInAccount)
        .setGravityForPopups(Gravity.TOP or Gravity.CENTER_HORIZONTAL)
    val gamesClient = Games.getGamesClient(this@MainActivity, googleSignInAccount)
    gamesClient.setViewForPopups(window.decorView.findViewById(android.R.id.content))

你可能知道如何在使用Jetpack Compose时设置setViewForPopups吗? - smbdy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接