获取对快照的引用。 Google Play游戏服务已保存的游戏

时间:2015-04-30 13:54:01

标签: google-play-games

       private PendingResult<Snapshots.CommitSnapshotResult> 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();

            // Commit the operation
            return Games.Snapshots.commitAndClose(mGoogleApiClient, snapshot, metadataChange);
        }

https://developers.google.com/games/services/android/savedgames

文档说在调用writeSnaphot之前必须获取对快照的引用。由于快照是一个界面,因此无法使用new创建。

如何获取快照参考?

谢谢!

P.S。我看到有一种方法可以通过按名称打开现有已保存的游戏来获取引用,但我想要获取的引用是针对新快照,目前没有现有快照,因此使用load函数可能无法成功编写新快照。

1 个答案:

答案 0 :(得分:7)

您可以使用不存在的文件名调用open来创建新快照。此代码段对open的结果使用.await(),因此您需要从AsyncTask或其他非UI线程调用它。 (有关详细信息,请参阅https://developers.google.com/games/services/android/savedgames):

private PendingResult<Snapshots.CommitSnapshotResult> writeSnapshot(String newSnapshotFilename,
       byte[] data, Bitmap coverImage, String desc) {

Snapshots.OpenSnapshotResult result =
   Games.Snapshots.open(mGoogleApiClient, newSnapshotFilename, true).await();
// Check the result of the open operation
  if (result.getStatus().isSuccess()) {
    Snapshot snapshot = result.getSnapshot();
    snapshot.getSnapshotContents().writeBytes(data);

    // Create the change operation
    SnapshotMetadataChange metadataChange = new
           SnapshotMetadataChange.Builder()
                .setCoverImage(coverImage)
                .setDescription(desc)
                .build();

    // Commit the operation
   return Games.Snapshots.commitAndClose(mGoogleApiClient, snapshot, metadataChange);
   }
   return null;
}