envío extra a requestLocationUpdates intentService rompe actualizaciones de ubicación
Tengo problemas para enviar una cadena adicional con miPendingIntent
que paso aLocationServices.FusedLocationApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, PendingIntent callbackIntent)
.
Parece que el nombre de usuario adicional que estoy poniendo en elIntent
está destrozando la ubicación querequestLocationUpdates
está tratando de pasar a miIntentService
comointent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED)
devolucionesnull
.
EDITAR
He intentado hacer unUser
clase que implementaParcelable
y poniéndolo como extra:
mRequestLocationUpdatesIntent.putExtra("username", new User(username));
y también he tratado de poner elParcelable User
dentro de unaBundle
como se sugiere mediante un comentario en este informe de errorhttps://code.google.com/p/android/issues/detail?id=81812:
Bundle userBundle = new Bundle();
userBundle.putParcelable("user", new User(username));
mRequestLocationUpdatesIntent.putExtra("user", userBundle);
a mi servicio:
Bundle userBundle = intent.getBundleExtra("user");
User user = userBundle.getParcelable("user");
String username = user.getUsername();
Sin embargo, ninguno de estos enfoques ha hecho ninguna diferencia. Cada vez que pongo algo extra en mi intento, la ubicación nunca se agrega al intento cuando ocurren las actualizaciones.
Yo configuro estoIntentService
para manejar actualizaciones de ubicación:
public class LocationUpdateService extends IntentService {
private final String TAG = "LocationUpdateService";
public LocationUpdateService() {
super("LocationUpdateService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent");
Bundle extras = intent.getExtras();
Log.d(TAG, "keys found inside intent: " + TextUtils.join(", ", extras.keySet()));
String username = intent.getStringExtra("username");
if (username != null) {
Log.d(TAG, "username: " + username);
} else {
Log.d(TAG, "username: null");
}
if (!intent.hasExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED)) {
Log.d(TAG, "intent does not have location :(");
}
Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
if (location == null) {
Log.d(TAG, "location == null :(");
}
Log.d(TAG, "latitude " + String.valueOf(location.getLatitude()));
Log.d(TAG, "longitude " + String.valueOf(location.getLongitude()));
...
}
}
Cuando el usuario hace clic en un botón, elstartLocationUpdates
se llama en mi actividad principal:
clase de actividad principal:
...
Boolean mLocationUpdatesEnabled = false;
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(LOCATION_UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(LOCATION_UPDATE_FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void startLocationUpdates() {
Log.d(TAG, "startng location updates...");
mLocationUpdatesEnabled = true;
if (mLocationRequest == null) {
createLocationRequest();
}
// create the Intent to use WebViewActivity to handle results
Intent mRequestLocationUpdatesIntent = new Intent(this, LocationUpdateService.class);
// create a PendingIntent
mRequestLocationUpdatesPendingIntent = PendingIntent.getService(getApplicationContext(), 0,
mRequestLocationUpdatesIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
// request location updates
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest,
mRequestLocationUpdatesPendingIntent);
Log.d(TAG, "location updates started");
}
protected void stopLocationUpdates() {
Log.d(TAG, "stopping location updates...");
mLocationUpdatesEnabled = false;
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient,
mRequestLocationUpdatesPendingIntent);
Log.d(TAG, "location updates stopped");
}
Todo esto funciona bien y bien; Cuando el usuario presiona el botón,toggleLocationUpdates
se llama, que llamaLocationServices.FusedLocationApi.requestLocationUpdates
que llama miLocationUpdateService
donde puedo obtener la ubicación.
El problema viene cuando traté de poner una cuerda extra en miIntent
usando Intent.putExtra (String, String):
clase de actividad principal:
...
protected void startLocationUpdates(String username) {
....
// create the Intent to use WebViewActivity to handle results
Intent mRequestLocationUpdatesIntent = new Intent(this, LocationUpdateService.class);
//////////////////////////////////////////////////////////////////
//
// When I put this extra, IntentService sees my username extra
// but the parcelableExtra `location` == null :(
//
//////////////////////////////////////////////////////////////////
mRequestLocationUpdatesIntent.putExtra("username", username);
...
}
...
EDITAR Había comenzado la siguiente oración como una declaración en lugar de una pregunta: "Estoy usando ..."
¿Estoy utilizando el enfoque correcto para enviar algunos datos adicionales a este manejo de actualización de ubicaciónIntentService
¿O hay una forma más sensata de hacerlo?
¿Es esto un error o simplemente una mala documentación?