Mein Kontrollfluss ist in einem IntentService
(ausgelöst durch einen GcmListenerService) und sollte jetzt den Standort des Benutzers abrufen. AlsrequestLocationUpdates fünf mal von IntentService gestartet
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)
null
zurückkehren könnte, sollte ich einige Standortaktualisierungen anfordern. Ich gehe davon aus, dass bei jedem Update der Standort aufgrund der GPS-Mechanik genauer ist als der vorhergehende. Ich nehme an, dass fünf Messungen/Updates für einen genauen Standort ausreichen sollten. Wie kann ich diese Logik in einem IntentService
implementieren? Die Klasse implementiert die Zuhörer Schnittstellen:
public class LocationIntentService extends IntentService
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
So kann ich einen Zähler in public void onLocationChanged(Location location)
und rufen LocationServices.FusedLocationApi.removeLocationUpdates()
nach fünf Updates nutzen könnten. Ich bin mir jedoch nicht sicher, ob ich Android vertrauen kann, dass das gleiche IntentService
so lange lebt und nicht vom Müllsammler entfernt wird, sobald onHandleIntent
fertig ist.
Dies ist der komplette Code, den ich bisher ohne die Logik bin nur fünf Updates zu sammeln:
public class LocationIntentService extends IntentService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleApiClient mGoogleApiClient;
public LocationIntentService() {
super("LocationIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
System.out.println(result.toString());
}
@Override
public void onConnected(Bundle connectionHint) {
LocationRequest mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(500);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int cause) {
}
private void displayLocation(Location location) {
System.out.println(location.toString());
DbHandler dbHandler = new DbHandler(getBaseContext());
double latitude = location.getLatitude();
double longitude = location.getLongitude();
double altitude = location.getAltitude();
float speed = location.getSpeed();
long time = location.getTime();
float accuracy = location.getAccuracy();
PersistedLocation persistedLocation = new PersistedLocation(time, latitude, longitude, altitude, accuracy, speed);
dbHandler.insertLocation(persistedLocation);
}
@Override
public void onLocationChanged(Location location) {
displayLocation(location);
}
}