我想在Android中收到GCM通知时获取用户的位置。
所以在我GCMIntentService extends GCMBaseIntentService
我这样做:从GCMIntentService获取Android位置
@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);
}
在syncLocation我做正确的事情来获得位置
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正常工作,如果它是从应用程序中调用。
但是,当我从通知onMessage()调用它时,它会进入该方法,但onLocationChanged永远不会被调用。我怀疑它与上下文有关。
任何人都可以请给我任何信息,为什么它不工作?
谢谢
谢谢,现在这一切都有道理。那么最好的解决方案是创建一个将从GCM调用的服务? –
@DanyY:是的,你需要一个常规的'服务',一个控制你的生命周期的服务,它可以获得定位,做你想做的任何事情,然后*关闭它。 – CommonsWare