在Google Play服务中创建SnapShot

时间:2018-07-08 17:10:25

标签: android google-play-services

我正在尝试利用Google Play服务的“已保存的游戏”,但是我真的很难写到云上。我可以让我的应用显示已保存的GamesUI,但由于我不知道如何制作快照而无法编写它。顶部代码示例位于onActivityResult中,当我尝试保存时,Toast可以工作。底部代码来自使用Google Play文档的writeSnapShot。我把 PleaseHelpMe 放在我认为我做错了的地方。

if (requestCode == RC_SAVED_GAMES) {
        if (intent != null) {
            if (intent.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA)) {
                // Load a snapshot.
                SnapshotMetadata snapshotMetadata =
                        intent.getParcelableExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA);
                mCurrentSaveName = snapshotMetadata.getUniqueName();


                Toast.makeText(MainActivity.this, "RC_SAVED_GAMES",
                        Toast.LENGTH_LONG).show();

                // Load the game data from the Snapshot
                // ...
                loadSnapshot();

            } 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
                // ...

                byte [] bytes = ByteBuffer.allocate(4).putInt(currentPlayersCashInHand).array();

                Snapshot ***PleaseHelpMeHere*** = processSnapshotOpenResult( ? ,
                        MAX_SNAPSHOT_RESOLVE_RETRIES);

                writeSnapshot(***PleaseHelpMeHere***,
                        bytes, bitmapCoverImage, "time stamp" );

                 /**
                 kind of working
                writeSnapshot(processSnapshotOpenResult(SnapshotsClient.SnapshotConflict.getConflictingSnapshot(),MAX_SNAPSHOT_RESOLVE_RETRIES),
                        bytes, bitmapCoverImage, "time stamp" );
                */
                Toast.makeText(MainActivity.this, "WRTIE_NEW_SNAPSHOT",
                        Toast.LENGTH_LONG).show();
            }
        }
    }

这是文档中的writeSnapShot

 private Task<SnapshotMetadata> 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);
}

请知道我看过android-basic-samples,但它与文档不符。

0 个答案:

没有答案