Geofire does not update background location

0

I'm using the GeoFire api to get the live location, I created the service to update in the background, but when the application is in the background it does not update the location in the firebase, it follows the service code

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

private GoogleApiClient mGoogleApiClient;
private LocationRequest request;

private static int UPDATE_INTERVAL = 5000;
private static int DISPLACEMENT = 10;
private int FATEST_INTERVAL = 5000;

private GeoFire geoFire;
private DatabaseReference currentUserRef;

public GPSService() {
}

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

@Override
public void onCreate() {
    super.onCreate();
    buildGoogleApiClient();
    updateFirebaseToken();
    DatabaseReference refEntregadores = FirebaseDatabase.getInstance().getReference("localizacaoAoVivo");
    geoFire = new GeoFire(refEntregadores);

    DatabaseReference onlineRef = FirebaseDatabase.getInstance().getReference().child(".info/connected");
    currentUserRef = FirebaseDatabase.getInstance().getReference("localizacaoAoVivo")
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid());
    onlineRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            currentUserRef.onDisconnect().removeValue();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onLocationChanged(Location location) {
    Common.mLastLocation = location;
}

@SuppressLint("MissingPermission")
@Override
public void onConnected(@Nullable Bundle bundle) {
    createLocationRequest();
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, request, this);
    new WorkerThread().start();
}

@Override
public void onConnectionSuspended(int i) {

}

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

}

class WorkerThread extends Thread {
    @SuppressLint("MissingPermission")
    @Override
    public void run() {
        FirebaseDatabase.getInstance().goOnline();

        Common.mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (Common.mLastLocation != null) {
            final double latitude = Common.mLastLocation.getLatitude();
            final double longitude = Common.mLastLocation.getLongitude();

            geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(),
                    new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {
                        @Override
                        public void onComplete(String key, DatabaseError error) {

                        }
                    });

            GeoQuery query = geoFire.queryAtLocation(
                    new GeoLocation(Common.mLastLocation.getLatitude(), Common.mLastLocation.getLongitude()), 1);
            query.removeAllListeners();

            query.addGeoQueryEventListener(new GeoQueryEventListener() {
                @Override
                public void onKeyEntered(String key, GeoLocation location) {

                }

                @Override
                public void onKeyExited(String key) {

                }

                @Override
                public void onKeyMoved(String key, GeoLocation location) {
                    geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(),
                            new GeoLocation(location.latitude, location.longitude), new GeoFire.CompletionListener() {
                                @Override
                                public void onComplete(String key, DatabaseError error) {

                                }
                            });
                }

                @Override
                public void onGeoQueryReady() {

                }

                @Override
                public void onGeoQueryError(DatabaseError error) {

                }
            });
        }
    }
}

private void updateFirebaseToken() {
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference tokens = db.getReference("Tokens");

    Token token = new Token(FirebaseInstanceId.getInstance().getToken());
    tokens.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .setValue(token);
}

private void createLocationRequest() {
    request = new LocationRequest();
    request.setInterval(UPDATE_INTERVAL);
    request.setFastestInterval(FATEST_INTERVAL);
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    request.setSmallestDisplacement(DISPLACEMENT);
}

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

@Override
public void onDestroy() {
    super.onDestroy();
    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }
    SPManager.setGpsRunning(this, false);
    SPManager.setGpsOnLine( this, false);
    stopSelf();
}
 }
    
asked by anonymous 01.04.2018 / 16:25

0 answers