【问题标题】:Google Play Games Services: Writing saved gamesGoogle Play 游戏服务:编写保存的游戏
【发布时间】:2019-04-25 22:22:40
【问题描述】:

由于 Google Play Games Service API 最近发生了变化,我不得不替换我的 Android 应用中所有已弃用的代码。我正在关注https://developers.google.com/games/services/android/savedgames 中的 Google 指南,但我不清楚如何将快照传递给写入要保存的数据的函数。

private Task writeSnapshot(Snapshot snapshot, byte[] data, Bitmap coverImage, String desc) { // 设置快照的数据负载 snapshot.getSnapshotContents().writeBytes(data); // 创建更改操作 SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder() .setCoverImage(封面图片) .setDescription(描述) 。建造(); SnapshotsClient 快照客户端 = Games.getSnapshotsClient(this, GoogleSignIn.getLastSignedInAccount(this)); // 提交操作 返回 snapshotsClient.commitAndClose(snapshot, metadataChange); }

你能帮帮我吗?我认为应该在文档中添加一个使用此功能的示例,以使一切更清晰,并帮助需要从头开始学习的开发人员。

【问题讨论】:

    标签: java android google-play-services google-play-games


    【解决方案1】:

    好的,我知道该怎么做。基本上,当您打开快照客户端时,您必须使用 continueWith 并从任务中获取快照。

    考虑到您有正确的封面图片和说明以及您登录的 Google 帐户

    mAccount = GoogleSignIn.getLastSignedInAccount(activity);
    

    这是代码:

    SnapshotsClient snapshotsClient = Games.getSnapshotsClient(activity, mAccount);
    int conflictResolutionPolicy = SnapshotsClient.RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED;
    snapshotsClient.open(getSaveFileName(), true, conflictResolutionPolicy)
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Error", e);
            }
        }).continueWith(new Continuation<SnapshotsClient.DataOrConflict<Snapshot>, byte[]>() {
            @Override
            public byte[] then(@NonNull Task<SnapshotsClient.DataOrConflict<Snapshot>> task) 
                    throws Exception {
                Snapshot snapshot = task.getResult().getData();
                snapshot.getSnapshotContents().writeBytes(getSaveGameData());
                SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
                    .setCoverImage(coverImage)
                    .setDescription(desc) 
                    .build();
                SnapshotsClient snapshotsClient = Games.getSnapshotsClient(activity, mAccount);
                snapshotsClient.commitAndClose(snapshot, metadataChange);
                return null;
            }
        });
    

    【讨论】:

    • 什么是getSaveFileName
    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2014-01-16
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    相关资源
    最近更新 更多