3

I have a service that will send location updates in a background thread. I have declared the service in the manifest as:

<service android:name="mypackage.LocationUpdater"></service>

I get my data from the activity in OnStartComand. But the OnLocationChanged is not called at all. I am unable to call the sendLocation() method from anywhere in the service.

public class LocationUpdater extends Service implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {

    LocationListener mLocationListener;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation; //this object should be used to request location updates
    String p = "test", d = "test";
    @Override
    public void onCreate() {
        buildGoogleApiClient();

    }

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

    protected void createLocationRequest() {
        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000);
        mLocationRequest.setFastestInterval(3000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        p = intent.getStringExtra("ptok");
        d = intent.getStringExtra("dtok");
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_NOT_STICKY;
    }


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

            mLocationRequest = LocationRequest.create();
            mLocationRequest.setInterval(5000);
            mLocationRequest.setFastestInterval(3000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

        if (mLastLocation != null) {
            String latitude = String.valueOf(mLastLocation.getLatitude());
            String longitude = String.valueOf(mLastLocation.getLongitude());
            sendLocation(p, latitude, longitude, d);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

            if(location != null) {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                Log.i("info", "Latitude :: " + latitude);
                Log.i("info", "Longitude :: " + longitude);
                //sending location details
                sendLocation(p, String.valueOf(latitude), String.valueOf(longitude), d);
            }
    }
}

I have to also add onConnected is never called as well. All I see is onStartComand being called and pass the values sent from my activity using intent.

The_Martian
  • 3,684
  • 5
  • 33
  • 61

1 Answers1

1

The docs didn't describe this but based on this SO accepted answer, I had to explicitly call mGoogleApiClient.connect(); in my onCreate method. You call that in onStart(); from activity.

Community
  • 1
  • 1
The_Martian
  • 3,684
  • 5
  • 33
  • 61