简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM