In my android app i want to get user current location every few minute interval and update in to my center server using web service. Currently i am using Fused Location Provide for get user current location, See link

now i want to know what is the best way to get user location frequently and call web service.

below is my code which gives me user current location: -

     locationrequest = LocationRequest.create();
     locationrequest.setInterval(10000);
     locationclient.requestLocationUpdates(locationrequest,new com.google.android.gms.location.LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
              Log.i(TAG, "Last Known Location :" + location.getLatitude() + "," + location.getLongitude());
        }
    });

now from where i have to call this code. Can i use this in a background service or some where else.

Please present your idea

TIA.

Best Answer


None of the rest of the answers use.

com.google.android.gms.location.FusedLocationProviderClient

Is google's fused location provider the main entry point for interaction with the fused location provider and it is very difficult to find a good example This was released by google in mid 2017

Google Play services location APIs are preferred over the Android framework location APIs (android.location)

If you currently use the android framework location apis you are strongly encouraged to switch to the google play services location apis as soon as possible

For you to use the Google Location API, first add this to your build.gradle

compile 'com.google.android.gms:play-services:11.0.0'

Then you can use this class Wherebouts.java .

import android.location.Location;
import android.os.Looper;
import android.util.Log;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;

/**
 * Uses Google Play API for obtaining device locations
 * Created by alejandro.tkachuk
 * alejandro@calculistik.com
 * www.calculistik.com Mobile Development
 */

public class Wherebouts {

    private static final Wherebouts instance = new Wherebouts();

    private static final String TAG = Wherebouts.class.getSimpleName();

    private FusedLocationProviderClient mFusedLocationClient;
    private LocationCallback locationCallback;
    private LocationRequest locationRequest;
    private LocationSettingsRequest locationSettingsRequest;

    private Workable<GPSPoint> workable;

    private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
    private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 1000;

    private Wherebouts() {
        this.locationRequest = new LocationRequest();
        this.locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        this.locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        this.locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(this.locationRequest);
        this.locationSettingsRequest = builder.build();

        this.locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult); // why? this. is. retarded. Android.
                Location currentLocation = locationResult.getLastLocation();

                GPSPoint gpsPoint = new GPSPoint(currentLocation.getLatitude(), currentLocation.getLongitude());
                Log.i(TAG, "Location Callback results: " + gpsPoint);
                if (null != workable)
                    workable.work(gpsPoint);
            }
        };

        this.mFusedLocationClient = LocationServices.getFusedLocationProviderClient(MainApplication.getAppContext());
        this.mFusedLocationClient.requestLocationUpdates(this.locationRequest,
                this.locationCallback, Looper.myLooper());
    }

    public static Wherebouts instance() {
        return instance;
    }

    public void onChange(Workable<GPSPoint> workable) {
        this.workable = workable;
    }

    public LocationSettingsRequest getLocationSettingsRequest() {
        return this.locationSettingsRequest;
    }

    public void stop() {
        Log.i(TAG, "stop() Stopping location tracking");
        this.mFusedLocationClient.removeLocationUpdates(this.locationCallback);
    }

}

From your Activity, you can use it like this, by passing a Workable object. A workable object is nothing more than a customized similar callback object

 Wherebouts.instance().onChange(workable);

By using a callback like workable, you will write ui related code in your activity and leave the hustle of working with gps to a helper class, like wherebouts.

    new Workable<GPSPoint>() {
        @Override
        public void work(GPSPoint gpsPoint) {
            // draw something in the UI with this new data
        }
    };

Of course you'd need to ask for the corresponding permissions from android os for your app to use You can read the following documentation for more information or do some research