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


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创建它。

如何获取对快照的引用?

谢谢!

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

您可以使用不存在的文件名调用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;
}

最新更新