我正在制作一个Android应用程序。如果设备静止并且移动了 300 或 400 m 的距离,我想停止位置更新,然后再次获取其位置。我不想连续检查位置更新并测量先前位置和当前位置之间的距离,因为它会消耗电池。 我的位置服务类代码:
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),但它不断接收位置更新,仅当先前位置和当前位置之间的距离小于 500m 时,才会调用 onLocationChanged 方法,例如
我应该为此目的使用地理围栏吗? 使用 FusedLocationApi 以最小的电池消耗实现此目的的最佳方法是什么?
最佳答案
这里有几点需要注意:
- 当您的设备未移动时,定位服务会优化电池电量,并且不一定会进行昂贵的新位置查找。因此,您实际上不必自己尝试对此进行优化。
- 考虑向
LocationRequest#setSmallestDisplacement
传递一个较小的值。 - 要优化电池电量,请考虑使用批量位置更新,其中按照 LocationRequest#setInterval 中描述的时间间隔为您计算位置。 ,但根据 LocationRequest#setMaxWaitTime 中的值传送到您的设备。这对电池有很大帮助。
不是您问题的一部分,但我应该注意,我会以稍微不同的方式构建请求和删除位置更新的代码。我将在 onStart()
中连接 GoogleApiClient
,并在 onStop()
中断开连接。我会在onConnected()
的onResume()
中调用requestLocationUpdates()
,并在onConnected()
中调用removeLocationUpdates()
onPause()
或 onStop()
,但不会晚于 onDestroy()
。
关于android - 如果设备没有移动,则停止位置更新,如果移动了 300m 的距离,则获取其位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42984155/