如何静默下载Google Play游戏服务保存的游戏快照的数据?



我正在尝试在我的Android应用程序中将Google的已保存游戏功能与Google Play游戏服务一起使用。

private static final int RC_SAVED_GAMES = 9009;
private String mCurrentSaveName = "snapshotTemp";
private void showSavedGamesUI() {
SnapshotsClient snapshotsClient =
Games.getSnapshotsClient(this, GoogleSignIn.getLastSignedInAccount(this));
int maxNumberOfSavedGamesToShow = 5;
Task<Intent> intentTask = snapshotsClient.getSelectSnapshotIntent(
"See My Saves", true, true, maxNumberOfSavedGamesToShow);
intentTask.addOnSuccessListener(new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityForResult(intent, RC_SAVED_GAMES);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (intent != null) {
if (intent.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA)) {
// Load a snapshot.
SnapshotMetadata snapshotMetadata =
intent.getParcelableExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA);
mCurrentSaveName = snapshotMetadata.getUniqueName();
// Load the game data from the Snapshot
// ...
} else if (intent.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_NEW)) {
// Create a new snapshot named with a unique string
String unique = new BigInteger(281, new Random()).toString(13);
mCurrentSaveName = "snapshotTemp-" + unique;
// Create the new snapshot
// ...
}
}
}

显然,Google希望您使用他们提供的意图让用户决定加载哪个保存的游戏,或者是否应该创建新的保存游戏。

另一方面,我想为用户做这个决定。但是,我找不到返回快照列表和加载快照数据的方法。

由于我的游戏不需要为每个用户维护多个已保存的游戏,因此我对获取没有意图的快照列表不太感兴趣(不过,这将是一个有趣的解决方案(,而更多的是基于保存的游戏名称加载快照,静默。

如何在不显示意图的情况下加载快照?

jess 的评论让我想到了一个现已弃用的解决方案。但是,发布答案的人指出,Google提供的CollectAllTheStars示例应用程序中也有一个有效的解决方案。我很想检查这个示例应用程序,以了解Google团队是否已更改代码以适应新方式。为了逗我开心,该示例应用程序中的评论仍然描述了旧的弃用方式,但为了我的运气而更改了代码。

检查代码给了我一些想法,所以我想出了这个解决方案:

String serializedSavedGameData;
public void downloadSavedGameData(final String name) {
if(snapshotsClient != null) {
snapshotsClient.open(name, true, SnapshotsClient.RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Error while opening snapshot: ", 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();
// Opening the snapshot was a success and any conflicts have been resolved.
try {
// Extract the raw data from the snapshot.
return snapshot.getSnapshotContents().readFully();
} catch (IOException e) {
Log.e(TAG, "Error while reading snapshot: ", e);
} catch (NullPointerException e) {
Log.e(TAG, "Error while reading snapshot: ", e);
}
return null;
}
}).addOnCompleteListener(new OnCompleteListener<byte[]>() {
@Override
public void onComplete(@NonNull Task<byte[]> task) {
if(task.isSuccessful()) {
byte[] data = task.getResult();
try {
serializedSavedGameData = new String(data, "UTF-16BE");
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "Failed to deserialize save game data: " + e.getMessage());
}
} else {
Exception ex = task.getException();
Log.d(TAG, "Failed to load saved game data: " + (ex != null ? ex.getMessage() : "UNKNOWN"));
}
}
});
}
}

我实施了一个简单的解决策略(以最新保存的冲突游戏为例(,但我没有时间硬测试所有不同的情况,如冲突,但它到目前为止通过了我的简单测试。

希望任何人都可以从中受益。

最新更新