sending extra to requestLocationUpdates intentService bricht Standortaktualisierungen ab
Ich habe Probleme beim Senden einer zusätzlichen Zeichenfolge mit meinemPendingIntent
das übergebe ich anLocationServices.FusedLocationApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, PendingIntent callbackIntent)
.
Es scheint, dass der Benutzername extra ich auf das @ setzIntent
entstellt den Ort, an demrequestLocationUpdates
versucht an mein @ zu übergebIntentService
wieintent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED)
kehrt zurücknull
.
BEARBEITE
Ich habe versucht, ein @ zu machUser
Klasse, die @ implementieParcelable
und als Extra setzen:
mRequestLocationUpdatesIntent.putExtra("username", new User(username));
und ich habe auch versucht, das @ zu setzParcelable User
in einemBundle
wie via Kommentar in diesem Fehlerbericht vorgeschlagenhttps: //code.google.com/p/android/issues/detail? id = 81812:
Bundle userBundle = new Bundle();
userBundle.putParcelable("user", new User(username));
mRequestLocationUpdatesIntent.putExtra("user", userBundle);
in meinem Dienst:
Bundle userBundle = intent.getBundleExtra("user");
User user = userBundle.getParcelable("user");
String username = user.getUsername();
Allerdings hat keiner dieser Ansätze einen Unterschied gemacht. Immer, wenn ich meine Absicht mit einem zusätzlichen Element versah, wird der Speicherort bei den Aktualisierungen nicht zu der Absicht hinzugefügt.
Ich richte das einIntentService
, um Standortaktualisierungen durchzuführen:
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()));
...
}
}
Wenn der Benutzer auf eine Schaltfläche klickt, wird dasstartLocationUpdates
wird in meiner Hauptaktivität aufgerufen:
Hauptaktivitätsklasse:
...
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");
}
Das alles funktioniert gut und gut; Wenn der Benutzer die Taste drückt, wirdtoggleLocationUpdates
heißt, das ruftLocationServices.FusedLocationApi.requestLocationUpdates
was mein @ nenLocationUpdateService
Wo kann ich den Standort finden?
Das Problem kommt, wenn ich versucht habe, eine zusätzliche Zeichenfolge auf mein @ zu legeIntent
using Intent.putExtra (String, String):
Hauptaktivitätsklasse:
...
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);
...
}
...
BEARBEITE Ich hatte den nächsten Satz eher als Aussage als als Frage begonnen: "Ich benutze ..."
Beim Verwenden des richtigen Ansatzes zum Senden einiger zusätzlicher Daten an diesen StandorIntentService
oder gibt es einen vernünftigeren Weg, dies zu tun?
Ist das ein Fehler oder nur eine schlechte Dokumentation?