如何每隔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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...