我想在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