【问题标题】:Obtaining a reference to a Snapshot. Google Play Games Services Saved Games获取对快照的引用。 Google Play 游戏服务保存的游戏
【发布时间】:2015-07-10 06:27:33
【问题描述】:
       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 创建。

如何获取对快照的引用?

谢谢!

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

【问题讨论】:

    标签: google-play-games


    【解决方案1】:

    您可以使用不存在的文件名调用 open 来创建新快照。这个 sn-p 对 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-19
      • 2019-04-25
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 2014-01-16
      相关资源
      最近更新 更多