【问题标题】:No Pop Up is displayed when Calling Games.Achievements.unlock调用 Games.Achievements.unlock 时不显示弹出窗口
【发布时间】:2014-09-02 12:52:51
【问题描述】:

我当前的 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() 以使您的整个应用程序永远不会看到上面显示的警告消息?

【问题讨论】:

    标签: android google-play-games


    【解决方案1】:

    我找到了解决方案,使用以下代码

    Games.setViewForPopups(getApiClient(), getWindow().getDecorView().findViewById(android.R.id.content));
    

    我可以显示弹出窗口。我现在有一个相关的问题。弹出窗口不会显示很长时间。

    我认为这是因为我在此活动中添加了自定义动画。

    有什么方法可以增加弹出窗口的可见时间?

    【讨论】:

    • 您在哪里使用此代码?我有同样的警告,但我发现在我的项目中使用此方法的唯一地方是 GPS 库的 Games 类。我应该在 BaseGameActivity 的某处手动使用此代码吗?谢谢。
    【解决方案2】:

    最近更新播放服务框架有一些变化。改用它,这样您也可以看到问候弹出窗口和解锁弹出窗口。

    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;
    

    【讨论】:

      【解决方案3】:

      这对我有用。

      @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。查看Get root view from current activity

      【讨论】:

        【解决方案4】:

        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's 对象

        【讨论】:

          【解决方案5】:

          对于那些在 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()
                  }
              }
          }
          

          【讨论】:

            【解决方案6】:

            这很好用(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))
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-06-03
              • 1970-01-01
              • 2020-10-12
              • 1970-01-01
              • 2022-12-14
              • 2015-06-27
              相关资源
              最近更新 更多