Android 设备与Google云消息之间的实时通信

Android 设备与Google云消息之间的实时通信,android,google-cloud-messaging,Android,Google Cloud Messaging,我从一年前开始是安卓开发者,但我从来没有做过任何在设备之间通信的应用程序(只是接听和接听电话) 我正在考虑用Android原生版(而不是Cocos2D-x或游戏引擎)制作一款纸牌游戏 设备之间的通信方式是GCM,但我不完全理解如何处理游戏逻辑 我知道类gcminentservice可以覆盖: @Override protected void onRegistered(Context context, String registrationId) { Log.i(TAG, "onRegi

我从一年前开始是安卓开发者,但我从来没有做过任何在设备之间通信的应用程序(只是接听和接听电话)

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

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

我知道类
gcminentservice
可以覆盖:

@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()
上开发所有游戏逻辑吗?你能给我举个例子吗


提前感谢。

如果您想开始活动(可能是您的游戏):


在已启动的活动情况下,使用
pendingent
代替,使用FLAG_UPDATE_CURRENT FLAG

onMessage只是GCM的一种广播方法,每当设备之间有消息时就会调用该方法,其余逻辑不需要在其中,但如果需要,可以从onMessage调用它们,与游戏互动的逻辑。。。例如,我收到一张{卡片:“4”}。。。我必须将其发送到我当前的活动/片段吗?很明显,您需要它的地方!但在整个活动中,我需要在他们之间进行沟通well@RafaFirenze:这个主题可能会帮助你:好吧,那就意味着我必须根据消息控制所有游戏的逻辑,不是吗?。例如,如果我在游戏中通知免费房间,如果你在房间里,如果你在玩。。。所有这些逻辑都是通过将数据从服务发送到意图来处理的。。。是吗?@Rafa Firenze:是的,那么你的活动可以基于意图和信息来做某些逻辑
@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); 
}