0

So, I have a JobService that is being called on background. However I need it to get the current location and send it to the webservice. The webservice is working great when I send static data. However getting the location is being a problem. The location object is always null. This is what I've been trying.

public class GPSBackgroundJobService extends JobService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private String LOGSERVICE = "GPS";

private InspectorsLogic mInspectorsLogic;
ParsoContext mContext;

private GoogleApiClient mGoogleApiClient;

ParsoLocationListener mLocationListener;
Location mLastLocation;
Double longitude;
Double latitude;



@Override
public void onCreate() {
    super.onCreate();
    Log.i(LOGSERVICE, "onCreate");
    mContext = (ParsoContext) getApplicationContext();
    mInspectorsLogic = new InspectorsLogic(mContext);


    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
}





@Override
public boolean onStartJob(JobParameters job) {
    Log.i(LOGSERVICE, "onStartJob called");
    mGoogleApiClient.connect();
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    if (mLastLocation!=null) {
        latitude = mLastLocation.getLatitude();
        longitude =mLastLocation.getLongitude();
        sendGPSTask mSendGPSTask = new sendGPSTask();
        mSendGPSTask.execute(Double.toString(latitude), Double.toString(longitude));
    }else{
        Log.e(LOGSERVICE, "Lat and Long are null");
    }
    return false;
}

@Override
public boolean onStopJob(JobParameters job) {
    mGoogleApiClient.disconnect();
    return false;
}

@Override
public void onConnected(Bundle connectionHint) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}


}
Diego Rivera
  • 403
  • 1
  • 5
  • 19

2 Answers2

0

getLastLocation returns null if it doesn't have a location fix yet- which means unless you're using GPS elsewhere, it almost always returns null. To make sure you get a location use getLocationUpdates and wait for the location to fix (this will not be instant, depending on atmospheric conditions, whether you're inside or out, etc this could take a few seconds to a minute or so- or just never happen).

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

Apparently the code is fine, to make it work this is what I did:

  1. check the permissions of the app (the location one needs to be activated)
  2. change your GPS settings to (mobile data and wifi one)

making this with the current code posted on the question, made it.

Diego Rivera
  • 403
  • 1
  • 5
  • 19