在Android上保存的游戏:如何检查具有相同名称的快照是否已存在? [英] Saved Games on Android: how to check if a snapshot with the same name already exists?
问题描述
目前,我正在将Google的已保存的游戏集成到Android应用.
Currently I am working on the Google's Saved Games integration into an Android app.
在用户请求新的保存后,我试图创建一个新的快照.我发现
I am trying to create a new snapshot after the user requests new save. I implemented onActivityResult
as i found here:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
// requestCode and resultCode checks happen here, of course...
if (intent != null) {
if (intent.hasExtra(Snapshots.EXTRA_SNAPSHOT_METADATA)) {
// Load a snapshot.
SnapshotMetadata snapshotMetadata = intent.getParcelableExtra(Snapshots.EXTRA_SNAPSHOT_METADATA);
currentSaveName = snapshotMetadata.getUniqueName();
loadFromSnapshot(snapshotMetadata);
} else if (intent.hasExtra(Snapshots.EXTRA_SNAPSHOT_NEW)) {
// Create a new snapshot named with a unique string
// TODO: check for existing snapshot, for now, add garbage text.
String unique = new BigInteger(281, new Random()).toString(13);
currentSaveName = "snapshotTemp-" + unique;
saveSnapshot(null);
}
}
}
很明显,检查是否存在具有生成名称的快照是一个好主意.我该怎么办呢?
Obviously it is a good idea to check if a snapshot with the generated name already exists. How should I actually do it?
推荐答案
The list of existing saved games can be retrieved by calling [Snapshots.load()](https://developers.google.com/android/reference/com/google/android/gms/games/snapshot/Snapshots#load(com.google.android.gms.common.api.GoogleApiClient, boolean)). This is an asynchrounous call, so one way to use it is to call it before saving and keep the names in a list which you can then compare to the new name.
CollectAllTheStars示例( https://github.com/playgameservices/android-basic-samples )演示了如何使用此API来显示自定义视图以选择已保存的游戏.
The sample CollectAllTheStars (https://github.com/playgameservices/android-basic-samples) demonstrates how to use this API to display a custom view to select a saved game.
Games.Snapshots.load(mGoogleApiClient, false).setResultCallback(
new ResultCallback<Snapshots.LoadSnapshotsResult>() {
@Override
public void onResult(Snapshots.LoadSnapshotsResult loadSnapshotsResult) {
mSavedGamesNames = new ArrayList<String>();
for (SnapshotMetadata m :loadSnapshotsResult.getSnapshots()) {
mSavedGamesNames.add(m.getUniqueName());
}
}
});
这篇关于在Android上保存的游戏:如何检查具有相同名称的快照是否已存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!