gpt4 book ai didi

android - 我无法理解如何实现 "Result Callback"。有人能指出我正确的方向吗?

转载 作者:行者123 更新时间:2023-11-29 20:56:02 25 4
gpt4 key购买 nike

我正在关注 this guide on google用于设置适用于 Android 的 Google Play 游戏服务。

在实现 ResultCallback 的部分,它说:

In the snippet, MatchInitiatedCallback is a class that implements the ResultCallback interface. You can attach this object to the GoogleApiClient so that your game is notified whenever a match is initiated. To see how the MatchInitiatedCallback is implemented, see Taking the first turn.

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”的类,但我不知道如何处理它。我想开始一个新的 Intent 。

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

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

我不太了解这些回调的事情,不知道从哪里开始,谁能给我指明正确的方向?

最佳答案

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

回调是使用调用主 Activity 方法的匿名类实现的。

    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

关于android - 我无法理解如何实现 "Result Callback"。有人能指出我正确的方向吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27736531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com