GCM interacting with google maps

Asked

Viewed 94 times

0

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

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

I just did in the onMessage call my function, but error, saying I’m not in UI Thread anyone knows how to do?

2 answers

1


When you use GCM, you declare your GcmIntentService and your 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);
}

0

(Just so you know, the method GCMBaseIntentService.onMessage() is obsolete (deprecated), in its place should use an appropriate broadcast receiver and register with the GCM server with the class Googlecloudmessaging).

Regarding your problem, throw a broadcast in the method receiving the message (in your case, onMessage() same) and make the screen of your map receive the broadcast.

*If* by chance occurs the error that says you are not in the UI Thread, execute the code snippet that launches the broadcast in the UI Thread with the help of a Handler, thus:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.