由于Google Play游戏服务API的最新更改,我被迫替换了Android应用中所有不推荐使用的代码。我正在按照https://developers.google.com/games/services/android/savedgames中的Google指南进行操作,目前尚不清楚如何将快照传递给此函数以写入要保存的数据。
private Task 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(); SnapshotsClient snapshotsClient = Games.getSnapshotsClient(this, GoogleSignIn.getLastSignedInAccount(this)); // Commit the operation return snapshotsClient.commitAndClose(snapshot, metadataChange); }
你能帮我吗?我认为应该在文档中添加一个使用此功能的示例,以使所有内容更加清晰,并帮助需要从头开始学习此功能的开发人员。
答案 0 :(得分:0)
好,我意识到该怎么做。基本上,打开快照客户端时,必须使用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;
}
});