0

I'm new for android development need to get gps location in background service without user interaction like for every 15 mins need to fetch the coordinates and send the results to server how can i achieve this. Then i tried fused location api syncadapter combination.It works, i'm getting coordinates but i need to use that location class in dedicated android-service how can i achieve this need to make that service run for ever 15 mins fetch the coordinates and send that result to server let me post my Google api client class.Please check below code i have tried.

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.text.DateFormat;
import java.util.Date;

/**
 * Created by 4264 on 14-10-2016.
 */

public class Locationlistener implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,LocationListener  {


    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mCurrentLocation;
    String mLastUpdateTime;
    private Context context;
    private static final long INTERVAL = 1000 * 10;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    public Locationlistener(Context c)
    {
        context=c;
        //show error dialog if GoolglePlayServices not available
        createLocationRequest();
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();
    }
    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }
    public String lat(){
        String lat=null;
        if(mCurrentLocation==null){
            Log.d("is null","null");
        }
        else {
            lat=String.valueOf(mCurrentLocation.getLatitude());
        }
        return lat;
    }

    protected void startLocationUpdates() {
        PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
        Log.d("started", "Location update started ..............: ");
    }
    public void updateUI() {
        Log.d("updated", "UI update initiated .............");
        if (null != mCurrentLocation) {
            String lat = String.valueOf(mCurrentLocation.getLatitude());
            String lng = String.valueOf(mCurrentLocation.getLongitude());
            Log.e("latw",lat);
            Log.e("Long",lng);

        } else {
            Log.d("", "location is null ...............");
        }
    }


    @Override
    public void onConnected(Bundle bundle) {
        Log.d("connected", "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
        startLocationUpdates();
    }


    @Override
    public void onConnectionSuspended(int i) {

    }




    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.d("failed", "Connection failed: " + connectionResult.toString());
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("locationchanged", "Firing onLocationChanged..............................................");
        mCurrentLocation = location;
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateUI();
    }
}

It works in syncadapter but i need to make this in service how can i achieve this and another doubt if user turn off the gps how can i get location is it possible with network location updates thanks in advance!!

Saveen
  • 4,120
  • 14
  • 38
  • 41
M.Yogeshwaran
  • 1,419
  • 4
  • 22
  • 48
  • Posted example here https://stackoverflow.com/questions/39314901/getting-latitude-and-longitude-in-30-seconds/39315097#39315097 – Saveen Jan 02 '18 at 05:13

1 Answers1

0

I would suggest you to use AlarmManager to wake up device and receive location as soon as possible (interval 0) then continue to sleep at each 15 minutes. So this approach better than non-stop service.

If user turns off the gps, you will be still getting (network provider) location updates but not accurate due to location settings aren't adequate. Also to solve this, you can use SettingsApi

When making a request to location services, the device's system settings may be in a state that prevents an app from obtaining the location data that it needs. For example, GPS or Wi-Fi scanning may be switched off.

blackkara
  • 4,900
  • 4
  • 28
  • 58