【问题标题】:I'm having trouble understanding how to implement a "Result Callback". Can someone point me in the right direction?我无法理解如何实现“结果回调”。有人可以指出我正确的方向吗?
【发布时间】:2015-02-28 10:51:56
【问题描述】:

我正在关注 this guide on google 为 Android 设置 Google Play 游戏服务。

在实现 ResultCallback 的部分它说:

在sn-p中,MatchInitiatedCallback是一个实现ResultCallback接口的类。您可以将此对象附加到 GoogleApiClient 以便在启动比赛时通知您的游戏。要了解 MatchInitiatedCallback 是如何实现的,请参阅先行一步。

public class MatchInitiatedCallback implements
    ResultCallback<TurnBasedMultiplayer.InitiateMatchResult> {

@Override
public void onResult(TurnBasedMultiplayer.InitiateMatchResult result) {
    // Check if the status code is not success.
    Status status = result.getStatus();
    if (status.isSuccess()) {
        showError(status.getStatusCode());
        return;
    }

    TurnBasedMatch match = result.getMatch();

    // If this player is not the first player in this match, continue.
    if (match.getData() != null) {
        showTurnUI(match);
        return;
    }

    // Otherwise, this is the first player. Initialize the game state.
    initGame(match);

    // Let the player take the first turn
    showTurnUI(match);
 }
}

我创建了一个名为“MatchInitiatedCallback”的类,但我不知道如何处理它。我想开始一个新的意图。

    // Let the player take the first turn
    showTurnUI(match);

我尝试在 showTurnUI() 处启动一个新意图,但我猜这个 MatchInitiatedCallback 不会让我启动一个新意图,因为它没有继承自 Activity...(?)

我不太了解这些回调的东西,不知道从哪里开始,有人能指出我正确的方向吗?

【问题讨论】:

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


【解决方案1】:

在 GitHub 中查看基于回合的示例:https://github.com/playgameservices/android-basic-samples/blob/master/BasicSamples/SkeletonTbmp/src/main/java/com/google/example/tbmpskeleton

回调是使用匿名类实现的,该类调用主活动上的方法。

    ResultCallback<TurnBasedMultiplayer.InitiateMatchResult> cb = new ResultCallback<TurnBasedMultiplayer.InitiateMatchResult>() {
        @Override
        public void onResult(TurnBasedMultiplayer.InitiateMatchResult result) {
            processResult(result);
        }
    };
    Games.TurnBasedMultiplayer.createMatch(mGoogleApiClient, tbmc).setResultCallback(cb);

可以在示例上下文中看到 processResult 的代码:https://github.com/playgameservices/android-basic-samples/blob/master/BasicSamples/SkeletonTbmp/src/main/java/com/google/example/tbmpskeleton/SkeletonActivity.java#L638

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2016-05-26
    • 2011-07-31
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多