我有一个定位应用,可以将位置发送到Firebase。它基于Google的Transport Tracker应用演示,并作为服务运行。它随机停止发送位置。如果拨打电话,它将立即将数据发送到Firebase,但可能会再次停止。我没有进行任何地理围栏操作。正如您将通过我的代码看到的那样,我的setInterval为3分钟(180000),setFastestInterval为1分钟(60000),setMaxWaitTime为5分钟(300000)。这是我的代码:
private void requestLocationUpdates() {
final LocationRequest request = new LocationRequest();
final String path = getString(R.string.firebase_path) + "/" + getString(R.string.transport_id);
request.setInterval(180000);
request.setFastestInterval(60000);
request.setMaxWaitTime(300000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(this);
int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permission == PackageManager.PERMISSION_GRANTED) {
// Request location updates and when an update is
// received, store the location in Firebase
client.requestLocationUpdates(request, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
Location location = locationResult.getLastLocation();
if (location != null) {
Log.d(TAG, "location update " + location);
ref.setValue(location);
}
}
}, null);
}
}
是否有一种方法可以强制它每3分钟发送一次更新而不停止?