私のコントロールフローはIntentService
(GcmListenerServiceによってトリガーされる)にあり、ユーザーの位置を取得するはずです。としてrequestLocationUpdateはIntentServiceから5回開始されました
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)
を返す可能性があります私はいくつかの場所の更新を要求する必要があります。私は、GPSのメカニックのために、以前のものよりも場所がより正確であるとみなします。私は5つの測定/更新が正確な場所に十分であるべきだと考えます。 IntentService
でロジックを実装するにはどうすればよいですか?クラスには、リスナー・インターフェースを実装します。
public class LocationIntentService extends IntentService
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
この方法で私はpublic void onLocationChanged(Location location)
にカウンタを使用し、5つのアップデート後LocationServices.FusedLocationApi.removeLocationUpdates()
を呼び出すことができます。しかし、同じIntentService
が長生きし、onHandleIntent
が完了するとすぐにガベージコレクタによって削除されないAndroidを信頼できるかどうかはわかりません。私はsetNumUpdates(int i)
とsetExpirationDuration(long l)
でこれを達成するために管理
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);
}
}