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();
     locationclient.requestLocationUpdates(locationrequest,new {

        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


Best Answer

None of the rest of the answers use.

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 ''

Then you can use this class .

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


 * Uses Google Play API for obtaining device locations
 * Created by alejandro.tkachuk
 * 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();

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        this.locationSettingsRequest =;

        this.locationCallback = new LocationCallback() {
            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)

        this.mFusedLocationClient = LocationServices.getFusedLocationProviderClient(MainApplication.getAppContext());
                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");


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


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>() {
        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