Saved Games on Android: how to check if a snapshot with the same name already exists?

廉价感情. 提交于 2019-12-24 16:09:55

问题


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?


回答1:


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.

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());
                     }
         }
});


来源:https://stackoverflow.com/questions/33671138/saved-games-on-android-how-to-check-if-a-snapshot-with-the-same-name-already-ex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!