我目前的Android游戏采用了BaseGameActivity。
我的游戏采用了成就,在需要时可以解锁。
但是,我并不总是看到与解锁事件相关的PopUps。
我知道弹出窗口只会在您第一次解锁成就时出现。
有些弹出窗口看起来不错,其他(从我游戏中的不同屏幕出现)从未出现过。
我需要做些什么来保证弹出窗口出现?
我觉得它与此警告有关:
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()以便整个应用程序看到上面显示的WARNING消息?
答案 0 :(得分:6)
我找到了一个解决方案,使用以下代码
Games.setViewForPopups(getApiClient(), getWindow().getDecorView().findViewById(android.R.id.content));
我可以弹出窗口显示。我现在有一个相关的问题。弹出窗口不会显示很长时间。
我认为这是因为我在此活动中有自定义动画。
有没有办法增加弹出窗口的显示时间?
答案 1 :(得分:4)
这对我有用。
@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
答案 2 :(得分:3)
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对象
答案 3 :(得分:0)
对于那些在科特林苦苦挣扎的人来说,这对我有用:
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()
}
}
}
答案 4 :(得分:0)
最近更新的播放服务框架进行了一些更改。请改用此选项,以便您可以看到问候语弹出窗口和解锁弹出窗口。
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 :(得分:0)
这很好用(科特琳):
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))