简体   繁体   中英

Google Play Games Services: Writing saved games

Due to the recent changes in the Google Play Games Service API I'm forced to replace all the deprecated code in my Android app. I'm following the Google guide in https://developers.google.com/games/services/android/savedgames and it's not clear for me how to pass the snapshot to this function that writes the data to be saved.

private Task writeSnapshot(Snapshot snapshot, byte[] data, Bitmap coverImage, String desc) {
    // Set the data payload for the snapshot
      snapshot.getSnapshotContents().writeBytes(data);
      // Create the change operation
      SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
          .setCoverImage(coverImage)
          .setDescription(desc)
          .build();
      SnapshotsClient snapshotsClient =
          Games.getSnapshotsClient(this, GoogleSignIn.getLastSignedInAccount(this));
      // Commit the operation
      return snapshotsClient.commitAndClose(snapshot, metadataChange);
}

Can you help me? I think an example of use of this function should be added to the documentation to make everything clearer and to help developers who need to learn this from scratch.

Ok, I realized how to do it. Basically, when you open the snapshot client, you must use continueWith and obtain the snapshot from the task.

Considering you have a proper cover image and description and a Google account where you signed in

mAccount = GoogleSignIn.getLastSignedInAccount(activity);

this is the code:

SnapshotsClient snapshotsClient = Games.getSnapshotsClient(activity, mAccount);
int conflictResolutionPolicy = SnapshotsClient.RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED;
snapshotsClient.open(getSaveFileName(), true, conflictResolutionPolicy)
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e(TAG, "Error", e);
        }
    }).continueWith(new Continuation<SnapshotsClient.DataOrConflict<Snapshot>, byte[]>() {
        @Override
        public byte[] then(@NonNull Task<SnapshotsClient.DataOrConflict<Snapshot>> task) 
                throws Exception {
            Snapshot snapshot = task.getResult().getData();
            snapshot.getSnapshotContents().writeBytes(getSaveGameData());
            SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
                .setCoverImage(coverImage)
                .setDescription(desc) 
                .build();
            SnapshotsClient snapshotsClient = Games.getSnapshotsClient(activity, mAccount);
            snapshotsClient.commitAndClose(snapshot, metadataChange);
            return null;
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM