如何解决如何每隔10秒将后台更新持续发送到后台服务中的Firebase数据库?
我正在尝试开发汽车追踪android应用程序,为此,我正在使用Firebase datqbase。我创建了一个后台服务,以在10秒的更新间隔后将位置更新发送到Firebase数据库,但是我的服务仅将位置发送到Firebase一次,我希望每10秒将位置保存到数据库中。 这是后台服务的代码。
MyService.java
public class MyService extends Service {
private DatabaseReference databaseReference;
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String id=user.getUid();
private FusedLocationProviderClient mFusedLocationClient;
public MyService() {
}
@Override
public void onCreate() {
mFusedLocationClient = getFusedLocationProviderClient(this);
//
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
String id= Objects.requireNonNull(user).getUid();
databaseReference = FirebaseDatabase.getInstance().getReference("Current Location").child(id).push();
startLocationUpdates();
}
@Override
public int onStartCommand( Intent intent,int flags,int startId) {
super.onStartCommand(intent,flags,startId);
Toast.makeText(this,"Service running",Toast.LENGTH_SHORT).show();
getLastLocation();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// Todo: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@SuppressWarnings("MissingPermission")
public void getLastLocation() {
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// GPS location can be null if GPS is switched off
if (location != null) {
onLocationChanged(location);
startLocationUpdates();
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NotNull Exception e) {
Log.w("log","error getting GPS location");
e.printstacktrace();
}
});
}
@Override
public void onDestroy() {
Toast.makeText(this,"Service stopped",Toast.LENGTH_SHORT).show();
}
public void onLocationChanged(Location location) {
LocationHelper helper= new LocationHelper(location.getLatitude(),location.getLongitude(),location.getSpeed());
FirebaseDatabase.getInstance().getReference("Current Location").child(id).push().setValue(helper).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(MyService.this,"Location Saved",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MyService.this,"Location Not Saved",Toast.LENGTH_SHORT).show();
}
}
});
}
protected void startLocationUpdates() {
long UPDATE_INTERVAL = 10000;
long FASTEST_INTERVAL = 5000;
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL);
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
// https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
settingsClient.checkLocationSettings(locationSettingsRequest)
} }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。