Reputation: 7041
I want to get the location of a user in Android when he receives a GCM notification.
so in my GCMIntentService extends GCMBaseIntentService
I'm doing this :
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message. Extras: " + intent.getExtras());
String message = getString(R.string.gcm_message);
displayMessage(context, message);
syncLocations(context);
}
In syncLocation I'm doing things correctly to get the location
public void syncLocations(Context context)
{
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
mLocationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 0,
mLocationListener);
}
public LocationListener mLocationListener = new LocationListener() {
@Override
public void onStatusChanged(
String provider,
int status,
Bundle extras)
{}
@Override
public void onProviderEnabled(
String provider)
{}
@Override
public void onProviderDisabled(
String provider)
{}
@Override
public void onLocationChanged(
Location location)
{
//HANDLE LOCATION
}
};
syncLocation works correctly if it is called from inside the app.
But when i call it from the notification onMessage(), it enters the method, but onLocationChanged is never called. I suspect it has something to do with the context.
Can anyone please give me any info why it isn't working ?
Thanks
Upvotes: 2
Views: 615
Reputation: 1007359
Can anyone please give me any info why it isn't working ?
Because GCMIntentService
is an IntentService
and goes away (via stopSelf()
) shortly after onMessage()
returns. You cannot reliably do things like register listeners for location changes from an IntentService
.
Also, bear in mind that the device may be asleep when the GCM message comes in. GCMIntentService
will keep the device awake long enough to call onMessage()
, but once onMessage()
returns, the device can fall back asleep.
Upvotes: 3