如果设备没有移动,则停止位置更新,如果移动距离为300米,则获取位置

时间:2017-03-23 18:27:38

标签: android geolocation location fusedlocationproviderapi android-fusedlocation

我正在制作一个Android应用程序。如果设备静止,我想停止位置更新,如果它移动300或400米的距离,那么再次获取它的位置。我不想连续检查位置更新并测量先前位置和当前位置之间的距离,因为它消耗电池。 我的位置服务类代码:

public class LocationService extends Service implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

private final class ServiceHandler extends Handler {
    public ServiceHandler(Looper looper) {
        super(looper);
    }
}

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("ServiceStartArguments",
            Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    // Get the HandlerThread's Looper and use it for our Handler
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
}

public void buildGoogleApiClient() {
    createLocationRequest();
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(500);
}

public void startLocationUpdates() {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}

protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
    mGoogleApiClient.disconnect();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
    buildGoogleApiClient();
    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    // The service is no longer used and is being destroyed
    stopLocationUpdates();
}

@Override
public void onLocationChanged(Location location) {
    System.out.println("Latitude: " + location.getLatitude());
    System.out.println("Longitude: " + location.getLongitude());
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    startLocationUpdates();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

我正在使用locationRequest.setSmallestDisplacement(500),但它连续接收位置更新,如果前一位置和当前位置之间的距离小于500米,则不会调用onLocationChanged方法,例如。

我应该为此目的使用地理围栏吗? 使用FusedLocationApi以最少的电池消耗实现此目的的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

这里有几点需要注意:

  1. 当您的设备未移动时,位置服务会针对电池进行优化,并且不一定会进行昂贵的新位置查找。所以,你真的不必为此自己进行优化。
  2. 考虑将较小的值传递给LocationRequest#setSmallestDisplacement
  3. 要优化电池,请考虑使用批量位置更新,其中按LocationRequest#setInterval中所述的时间间隔为您计算位置,但会根据LocationRequest#setMaxWaitTime中的值将其传送到您的设备。这对电池有很大帮助。
  4. 不是您的问题的一部分,但我应该注意,我会以不同的方式构建用于请求和删除位置更新的代码。我会在GoogleApiClient中关联onStart(),并在onStop()中将其断开连接。我会在requestLocationUpdates()的{​​{1}}内拨打onResume(),并在onConnected()removeLocationUpdates()中致电onPause(),但不要迟到onStop() }}