请求位置更新五次从意图服务开始



我的控制流位于IntentService(由 GcmListenerService 触发(中,现在应该获取用户的位置。如

LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)

可能会返回null,我应该请求一些位置更新。由于GPS机制,我假设每次更新的位置都比以前的位置更准确。我想五次测量/更新应该足以获得准确的位置。如何在IntentService中实现该逻辑?该类实现侦听器接口:

public class LocationIntentService extends IntentService
 implements GoogleApiClient.ConnectionCallbacks,
 GoogleApiClient.OnConnectionFailedListener,
 LocationListener

这样,我可以在public void onLocationChanged(Location location)中使用计数器,并在五次更新后调用LocationServices.FusedLocationApi.removeLocationUpdates()。但是,我不确定我是否可以相信Android,相同的IntentService寿命那么长,并且一旦完成onHandleIntent就不会被垃圾收集器删除。

这是我到目前为止使用的完整代码,没有只收集五个更新的逻辑:

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);
    }
}

我设法用setNumUpdates(int i)setExpirationDuration(long l)完成了这项工作。

@Override
public void onConnected(Bundle connectionHint) {
    LocationRequest mLocationRequest = LocationRequest.create();
    mLocationRequest.setInterval(500);
    mLocationRequest.setNumUpdates(5);
    mLocationRequest.setExpirationDuration(10000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
}

相关内容

  • 没有找到相关文章

最新更新