0

I'm Android Developer since a year ago, but I've never done any app which communicates between devices (just GET and POST calls).

I'm thinking about making a card game in Android Native (not Cocos2D-x or a game engine).

The way to communicate between devices is GCM, but I didn't fully understand how I can handle the game logic.

I know that the class GCMIntentService can Override:

@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);
}

My question is: do I have to develop all the game logic on onMessage() ? could you provide me an example?

Thanks in advance.

Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
  • 2
    onMessage is just a method of your broadcast for GCM which gets called whenever there is a message from/between devices, the rest of the logic not need to be in there, but you can call them from onMessage if you require – Saqib Jun 23 '14 at 09:30
  • I mean, the logic which interacts with the game... for example I receive a {card:"4"}... do I have to send it to my current activity/fragment ? – Rafael Ruiz Muñoz Jun 23 '14 at 10:03
  • it's obvious where you need it! – Saqib Jun 23 '14 at 11:22

1 Answers1

0

If you want to start the Activity (probably the game in your case):

@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); 
}

In a started activity case, use PendingIntent instead, with FLAG_UPDATE_CURRENT flag

hungr
  • 2,086
  • 1
  • 20
  • 33
  • But along the activity I would need to communicate between them as well – Rafael Ruiz Muñoz Jun 23 '14 at 10:46
  • @Rafa Firenze: this topic may help you: http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging – hungr Jun 23 '14 at 10:49
  • Ok, then that means that I have to control all the game's logic depending of the message, no?. For example, if I'm notificating the free rooms on my game, if you are inside a room, if you are playing... all that logic is handled right there by sending data from Service to Intent... is that right? – Rafael Ruiz Muñoz Jun 23 '14 at 11:32
  • @Rafa Firenze: true, then your activities can base on the intent and the message to do certain logic – hungr Jun 23 '14 at 15:37