How can I use firebaseJobDispatcher to continuously update the location?

Level
I'm New

Task
To start the android service, which periodically sends location updates (latitude, longitude) to the database while the driver is driving (to track location) every 5 minutes. I want to update the database with the location, even if the application is inactive, so I decided to use firebase task manager.

Note
I have already seen several topics on how to do this using ALARM_MANAGER or broadcast receivers, etc. I would like to know how to do this with the task manager. My current code works, but I'm sure this is not the right way.

Questions
1. Where can I call to create Googleapi and call the Connect () method? Right now, my scheduler is calling the 'onStart' class, which takes turns connecting.

  1. Instead of using jobScheduler, is there a way I can use the “Location Receiver” for this when I read, it takes a time interval and returns the updated location, overwriting onLocationChanged().

Current working code

Firebase service: -

import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;

public class GPSTracking extends JobService  {
    private AsyncTask mBackgroundTask;

    public static final String LOG_TAG = "location-scheduler";

    @Override
    public boolean onStartJob(final JobParameters jobParameters) {
        Log.i(LOG_TAG,"Scheduling");

        mBackgroundTask = new AsyncTask() {

            @Override
            protected Object doInBackground(Object[] params) {
                Context context = GPSTracking2.this;

                LocationTracker.execute(context);
                return null;
            }

            @Override
            protected void onPostExecute(Object o) {
                jobFinished(jobParameters, false);
            }
        };

        mBackgroundTask.execute();
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        if (mBackgroundTask != null) {
            mBackgroundTask.cancel(true);
        }
        return true;
    }
}

Actual Tracking: -

import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;


public class LocationTracker implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    private Context context;

    private static final String LOG_TAG="LOCATION TRACKER";

    public LocationTracker(Context context){
        this.context = context;
        buildGoogleApiClient();
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        //Disconnect
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

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

    public static void execute(Context context){
        LocationTracker2 track = new LocationTracker2(context);
        //connect
        track.startConnection();

    }

    public void startConnection(){
        mGoogleApiClient.connect();
    }


    private static void trackLocation(Location location) {
// To replace with updating database
            if (location!=null) {
                Log.i("TRACKING LAT", String.valueOf(location.getLatitude()));
                Log.i("TRACKING LON", String.valueOf(location.getLongitude()));
        }
        else
        {
            Log.i("TRACKING LAT", "Null location received");

        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        // Create new location request
        // The permission should be granted previously
        if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        trackLocation(mLastLocation);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(LOG_TAG,"Connection suspended");

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.i(LOG_TAG,"Connection failed");
    }

}

Inside Main, start planning:

 Driver driver = new GooglePlayDriver(context);
        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(driver);
        Job locationTrackerJob = dispatcher.newJobBuilder()
                .setService(GPSTracking.class)
                .setTag(JOB_TAG)
                .setLifetime(Lifetime.FOREVER)
                .setRecurring(true)
                .setTrigger(Trigger.executionWindow(
                        REMINDER_INTERVAL_SECONDS,
                        REMINDER_INTERVAL_SECONDS + SYNC_FLEXTIME_SECONDS
                ))
                .setReplaceCurrent(true)
                .build();

        dispatcher.schedule(locationTrackerJob);

Note
- You may need to add code to check permissions depending on the api
- I leave the service registration in the manifest and Android dependencies in the gradle assembly.

+4

Source: https://habr.com/ru/post/1676422/


All Articles