Google Play游戏服务:编写存档

3
由于Google Play Games Service API的最近更改,我被迫替换Android应用程序中所有已弃用的代码。我正在按照Google指南https://developers.google.com/games/services/android/savedgames进行操作,但我不清楚如何将快照传递给此函数以写入要保存的数据。
private Task writeSnapshot(Snapshot snapshot, byte[] data, Bitmap coverImage, String desc) {
    // 为快照设置数据负载
      snapshot.getSnapshotContents().writeBytes(data);
      // 创建更改操作
      SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
          .setCoverImage(coverImage)
          .setDescription(desc)
          .build();
      SnapshotsClient snapshotsClient =
          Games.getSnapshotsClient(this, GoogleSignIn.getLastSignedInAccount(this));
      // 提交操作
      return snapshotsClient.commitAndClose(snapshot, metadataChange);
}
你能帮我吗?我认为应该在文档中添加此函数的使用示例,以使一切更清晰,并帮助需要从头学习此内容的开发人员。
1个回答

1

好的,我明白如何做了。基本上,当您打开快照客户端时,您必须使用continueWith并从任务中获取快照。

假设您有适当的封面图像和描述以及已登录Google帐户

mAccount = GoogleSignIn.getLastSignedInAccount(activity);

这是代码:

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

什么是 getSaveFileName - Abhay Koradiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接