获取对快照的引用. Google Play游戏服务已保存的游戏 [英] Obtaining a reference to a Snapshot. Google Play Games Services Saved 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之前获取对快照的引用.由于快照是一个接口,因此无法使用新快照创建.
The docs say that a reference to a Snapshot has to be obtained before calling writeSnaphot. Since snapshot is an interface it can't be created with new.
如何获取对快照的引用?
How to obtain a reference to a Snapshot?
谢谢!
P.S.我看到有一种方法可以通过按名称打开现有保存的游戏来获取引用,但是我想获取的引用是针对新快照的,当前没有现有快照,因此使用加载功能可能无法成功编写新的快照.
P.S. I see that there is a way to obtain a reference by opening an existing saved game by name however the reference I would like to obtain is for a new Snapshot there are currently no existing snapshots so using the load function probably will not succeed in writing a new snapshot.
推荐答案
您可以使用不存在的文件名调用open来创建新快照.此代码段对open的结果使用.await(),因此您需要从AsyncTask或其他一些非UI线程调用它. (请参见 https://developers.google.com/games/services/android/savedgames 了解更多信息):
You can call open with a filename that does not exist to create a new snapshot. This snippet uses .await() on the result from open, so you'll need to call it from an AsyncTask or some other non-UI thread. (see https://developers.google.com/games/services/android/savedgames for more details):
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;
}
这篇关于获取对快照的引用. Google Play游戏服务已保存的游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!