Search code examples
androidgoogle-playachievements

Google Play Achievement offline unlock and sync


The Google Play Documentation states:

An achievement can be unlocked offline. When the game comes online, it syncs with the Google Play games services to update the achievement's unlocked state.

I tried unlocking an achievement offline using:

Games.Achievements.unlock(mGoogleApiClient, achievementID);

However, I received the following error:

java.lang.IllegalStateException: GoogleApiClient is not connected yet. at com.google.android.gms.internal.jx.a(Unknown Source) at com.google.android.gms.common.api.c.b(Unknown Source) at com.google.android.gms.games.internal.api.AchievementsImpl.unlock(Unknown Source)

To prevent this, I wrapped the unlock statement with a check for connection:

if (mGoogleApiClient.isConnected()) {
        Games.Achievements.unlock(mGoogleApiClient, achievementID);
}

which is the solution used in the example provided by Google Play Games:

 if (mGoogleApiClient.isConnected()) {
      // unlock the "Trivial Victory" achievement.
      Games.Achievements.unlock(mGoogleApiClient,
          getString(R.string.achievement_trivial_victory));
 }

This solves the error issue. However, I do not know how this ensures that the achievement is successfully unlocked offline and synced when the device goes online, as is assured in the documentation I quoted above. It seems to me that the unlock event will simply be ignored because mGoogleApiClient.isConnected() returns false when offline and there is no way for Google Play Games to know that the achievement was unlocked offline once it goes online since no information about the unlock event was stored offline. (there is no "else" block statement after the "if" block statement, in which the offline achievement unlock would be handled)

Am I missing something? Is it the developer's responsibility to store information about achievement unlock when the device is offline and then tell Google Play to unlock the achievement when the device goes online?


Solution

  • The missing link is that you can call mGoogleApiClient.connect() while offline - Google Play Games will connect even when offline, allowing you to call unlock() and any other APIs.