Ich möchte den Standort des Benutzers alle paar Minuten im Hintergrund aktualisieren.
ich verwendet, um dieses Beispielcode googlesamples/android-play-location
aber ändert es mit Service
Ort im Hintergrund aktualisieren und LocationSettings überprüfen
public class MyService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener, ResultCallback<LocationSettingsResult>
zu verwenden, aber ich kann nicht Standorteinstellungen
protected void checkLocationSettings() {
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(
mGoogleApiClient,
mLocationSettingsRequest
);
result.setResultCallback(this);
}
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
startLocationUpdates();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
"upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
"not created.");
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
Log.i(TAG, "User agreed to make required location settings changes.");
startLocationUpdates();
break;
case Activity.RESULT_CANCELED:
Log.i(TAG, "User chose not to make required location settings changes.");
break;
}
break;
}
}
überprüfen Da diese Methode eine Activity
erfordertstatus.startResolutionForResult (MainActivity.this, REQUEST_CHECK_SETTINGS);
und wenn ich den vorherigen Code in der MainActvity setzen, habe ich nicht die Verweise auf mGoogleApiClient und mLocationSettingsRequest
LocationServices.SettingsApi.checkLocationSettings (mGoogleApiClient, mLocationSettingsRequest);
Ich möchte im Hintergrund die Position aktualisieren und speichern, auch wenn die Aktivität zerstört wird.
Verwenden Sie den Service ist die richtige Methode, dies zu tun?
Und wie überprüfe ich die Standorteinstellungen?
[EDIT]
Ich versuchte, die PendingIntent
und ein IntentService
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, mPendingIntent);
Diese Methode eignet sich für den Hintergrund Anwendungsfälle zu verwenden, insbesondere für den Empfang von Standortaktualisierungen, auch wenn die App hat wurde durch das System getötet.
Was ist der Unterschied zu der Version mit den Service
und LocationListner
?
Ich habe den LocationListener und die FusedLocationAPI sehr gut funktioniert.Mein Problem ist mit checkLocationSettings. –