【问题标题】:Real Time Communication between Devices and Google Cloud Messaging设备和谷歌云消息传递之间的实时通信
【发布时间】:2023-03-06 23:47:02
【问题描述】:

我是一年前的 Android 开发人员,但我从未开发过任何在设备之间进行通信的应用程序(只是 GET 和 POST 调用)。

我正在考虑用 Android Native(不是 Cocos2D-x 或游戏引擎)制作一款纸牌游戏。

设备之间的通信方式是 GCM,但我没有完全理解如何处理游戏逻辑。

我知道GCMIntentService这个类可以重写:

@Override
protected void onRegistered(Context context, String registrationId) {

    Log.i(TAG, "onRegistered: registrationId=" + registrationId);
}

@Override
protected void onUnregistered(Context context, String registrationId) {

    Log.i(TAG, "onUnregistered: registrationId=" + registrationId);
}

@Override
protected void onMessage(Context context, Intent data) {


}

@Override
protected void onError(Context arg0, String errorId) {

    Log.e(TAG, "onError: errorId=" + errorId);
}

我的问题是:我必须在onMessage() 上开发所有游戏逻辑吗?可以举个例子吗?

提前致谢。

【问题讨论】:

  • onMessage 只是 GCM 广播的一种方法,只要有来自/设备之间的消息,就会调用它,其余逻辑不需要在那里,但您可以从 onMessage 调用它们如果你需要
  • 我的意思是,与游戏交互的逻辑...例如我收到一张 {card:"4"}... 我必须将它发送到我当前的活动/片段吗?
  • 你需要它的地方很明显!

标签: android google-cloud-messaging


【解决方案1】:

如果您想启动 Activity(可能是您的游戏):

@Override
protected void onMessage(Context context, Intent data) {

    Intent intent= new Intent(context, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    intent.setAction(Long.toString(System.currentTimeMillis()));
    intent.putExtra(xxx,xxx); //try to put the require information from data intent
    context.startActivity(intent); 
}

在已启动的活动案例中,请改用PendingIntent,并带有 FLAG_UPDATE_CURRENT 标志

【讨论】:

  • 但是在活动中我还需要在他们之间进行交流
  • @Rafa Firenze:这个话题可能对你有帮助:stackoverflow.com/questions/4300291/…
  • 好的,那意味着我必须根据消息控制所有游戏的逻辑,不是吗?例如,如果我在我的游戏中通知空闲房间,如果你在房间里,如果你正在玩……所有这些逻辑都是通过从 Service 发送数据到 Intent 来处理的……对吗?
  • @Rafa Firenze:是的,那么你的活动可以根据意图和消息做一定的逻辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多