With the new GcmListenerService i'm trying to get the user location when a message is received :
public class MobilAirGcmListenerService extends GcmListenerService {
private static final String TAG = "MobilAir:GcmIService";
@Override
public void onMessageReceived (String from, Bundle data) {
final String message = data.getString("appehour");
// Post notification of received message.
lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, updateTime, distance, this );
Log.i(TAG, "Received: " + message);
Intent intentToBroadCast = new Intent(this, MobilAirNotificationClick.class);
sendBroadcast(intentToBroadCast);
}
}
but when de locationManger is called i get this error : Can't create handler inside thread that has not called Looper.prepare()
is onMessageReceived a thread ?
Thank you
onMessageReceived() is invoked on a thread pool in order to: 1. Not block the main thread, so that long running work can be done directly without having to start a new service. 2. Make it so one message that takes a long time to process won't block other (potentially real time) messages from being handled.
However the threads in the pool are raw Java threads, and don't have an associated looper, which you need to get location updates. You can start requesting location updates on the main looper via creating and posting to a handler:
new Handler(Looper.getMainLooper()).post(...)
Or if you want to get location updates for all messages, you could register your listener in onCreate().
But I don't think this is going to do what you want. As soon as you have finished processing the message (i.e. get to the end on onMessageReceived()), the GcmListenerService will stop itself (unless another message already arrived), and then you won't get your location updates any more as I assume the listener will be unregistered in onDestroy().