GCM interacting with google maps

0

I have a function that server to update my map in the application, ie change the position icons and such, but I would like to do the following:

When I received a message from my GCM, for example "Update", I would like the function to update my map to be called. Of course I need to check if the map screen is open and everything, but I do not know how to do it.

I simply did in onMessage call my function, but of the error, speaking that I am not in the UI Thread does anyone know how to do?

    
asked by anonymous 04.08.2014 / 00:09

2 answers

1

When you use GCM, you declare your GcmIntentService and its GcmBroadCastReceiver as described HERE

But you can also declare a BroadCastReceiver in your Activity:

private BroadcastReceiver GcmReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // chama seu método para atualizar o googleMaps
    }
};

@Override
protected void onStart() {
    super.onStart();
        registerReceiver(GcmReceiver, new IntentFilter(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE));
}

@Override
protected void onStop() {
    super.onStop();
        unregisterReceiver(GcmReceiver);
}
    
04.08.2014 / 17:34
0

(Just to warn, the GCMBaseIntentService.onMessage() method is deprecated ), instead you must use an appropriate broadcast receiver and register with the GCM server with class GoogleCloudMessaging ).

With regard to your problem, cast a broadcast on the method that receives the message (in this case, onMessage() same) in>.

* If * error occurs that says you are not in the UI Thread , run the code snippet that throws the broadcast > in the UI Thread with the help of a handler , like this:

public onMessage(Context context, Intent intent) {

    new Handler(Loooper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            Intent outroIntent = ...
            context.sendBroadcast(outroIntent); // Obs.: não confunda o intent recebido com
                                                // o intent que você quer mandar no broadcast
        }
    });
}
    
04.08.2014 / 16:51