No Pop Up is displayed when Calling Games.Achievem

2020-02-26 07:35发布

问题:

My current Android game employs BaseGameActivity.

My game employs Achievements, which are getting unlocked when required.

However i do not ALWAYS see the PopUps related to the unlock event.

I know the popup only appears when you first unlock the Achievement.

Some popups appear fine, others (from differnt screens within my game) never appear.

What do i have to do to Guarantee the popups appear?

I have a feeling its related to this WARNING:

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.

I have called setViewForPopups() from within the activity my popups do not display in, however i have never seen them.

How do you call setViewForPopups() so that you entire application nevers sees the WARNING messages shown above?

回答1:

I have found a solution, by using the following code

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

I can get popups to show. I now have a related issue. The popup doesn't display for very long.

I think this is due to the fact I have a custom animation into this activity.

Is there any way to increase how long a popup is visible?



回答2:

This worked for me.

@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 gives you the root element of a view, without having to know its actual name/type/ID. Check out Get root view from current activity



回答3:

Games.setViewForPopups is deprecated

To show popup, add the next code into your activity or fragment layout:

 <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" />

And add the next code after the code where you init your object of AchievementsClient class

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

where googleSignInAccount is GoogleSignInAccount's object



回答4:

There has been some changes in recent update to play services framework. Use this instead so that you can see greeting popup and unlock popups as well.

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

Don't forget to import as required like-

import android.view.Gravity;
import com.google.android.gms.games.GamesClient;


回答5:

For those of you struggling in Kotlin this is what works for me:

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()
        }
    }
}