Google Play已保存的游戏快照未初始化

时间:2015-07-17 11:19:56

标签: android google-play-services

我正在尝试使用Google Play保存的游戏功能。但无法找到初始化Sanpshot对象的方法。

Snapshot snapshot = null;
PendingResult<Snapshots.CommitSnapshotResult> result = writeSnapshot(snapshot, sendData, null, "saving game using Saved Game Api");

由于Snapshot snapshot = new Snapshot()不起作用,我该如何初始化对象?

1 个答案:

答案 0 :(得分:0)

这对我有用:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {

            case RC_SAVED_GAMES_UI:
                if (data != null) {
                    if (data.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA)) {
                        // Load a snapshot.
                        SnapshotMetadata snapshotMetadata = data.getParcelableExtra(SnapshotsClient.EXTRA_SNAPSHOT_METADATA);
                        assert snapshotMetadata != null;
                        sCurrentSaveName = snapshotMetadata.getUniqueName();

                        // Load the game data from the Snapshot
                        onLoadGame(sCurrentSaveName);
                    } else if (data.hasExtra(SnapshotsClient.EXTRA_SNAPSHOT_NEW)) {
                        // Create a new snapshot named with a unique string.
                        String unique = new BigInteger(281, new Random()).toString(13);
                        sCurrentSaveName = "snapshotTemp-" + unique;

                        // Create the new snapshot
                        onSaveGame(sCurrentSaveName);
                    }
                }
                break;

            default:
                break;
        }

    }

 private void onSaveGame(String currentSave) {
        // Display a progress dialog
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        assert account != null;
        SnapshotsClient sClient = Games.getSnapshotsClient(this, account);

        sClient.open(currentSave, true).addOnCompleteListener(task -> {
            //noinspection ConstantConditions
            Snapshot snapshot = task.getResult().getData();

            if (snapshot != null) {
                try {
                    Bitmap coverImage = BitmapFactory.decodeResource(getResources(), R.drawable.img_cover);
                    String desc = getString(R.string.text_save_desc, getDate());

                    onWriteSnapshot(snapshot, onConvertToBytes(uUser), coverImage, desc).addOnCompleteListener(task1 -> {
                        if (task1.isSuccessful()) {
                            showToast(this, 1, getString(R.string.text_save_success));
                        } else {
                            Log.e(TAG, "ERROR: " + task1.getException());
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        assert account != null;
        SnapshotsClient sClient = Games.getSnapshotsClient(this, account);

        // Commit the operation
        return sClient.commitAndClose(snapshot, metadataChange);
    }