3

Disclaimer: I know there are other questions similar but I want to try and create a super simpler case for others to recreate the problem

I've been using something very similar to the code below to request location updates using google play services for the past few months and its worked fine. Now for some reason onLocationChanged is no longer called. Happens across a whole range of test devices. I'm now using Play Services 7.0.0. I request updates and nothing happens. I suspect its related to a recent code update I made (albeit one that had nothing to do with the location system) but I'm clueless to find the fix. When I revert the commit, I still have the problem -- on old code that worked fine!

I've set this code into a totally new sample application and played with all the options for the locationRequest object for fun but nothing... Everything stops after the log, "onConnected, requestLocationUpdates" after onConnected is called.

Thanks for the help!

Similar:

  1. Android GPS onLocationChanged never called
  2. android onLocationChanged never called
  3. onLocationChanged() never called

Relevant Manifest Entries:

<application
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
     .....
</application>

<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Sample Activity:

public class MyActivity extends Activity {

Context act = this;
private GoogleApiClient mGoogleApiClient;
String TAG = "LocationService.java";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    new MyLocationCallbacks().buildGoogleApiClient();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private class MyLocationCallbacks implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

    public void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(act)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        //Log.i(TAG, "Connected yet? " + mGoogleApiClient.isConnecting() + " " + mGoogleApiClient.isConnected());
        mGoogleApiClient.connect();
        //Log.i(TAG, "Connected yet? " + mGoogleApiClient.isConnecting() + " " + mGoogleApiClient.isConnected());
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.i(TAG, "Connected to GoogleApiClient");
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setExpirationDuration(30000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        Log.d(TAG, "onConnected, requestLocationUpdates");
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
    }

    @Override
    public void onLocationChanged(Location locationA) {
        Log.d(TAG, locationA.getLongitude() + " :: " + locationA.getLatitude());
    }


    @Override
    public void onConnectionSuspended(int i) {
        Log.d(TAG, "onConnectionSuspended called");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d(TAG, "onConnectionFailed called");
    }

}

}

Community
  • 1
  • 1

1 Answers1

3

I like how I post a question just before finding the answer...

The above code works fine, I just didn't set an interval. on the LocationRequest, just call setInterval(whatever needed)... goodnight.

https://github.com/googlesamples/android-play-location

  • 2
    It used to be that when the device picked up its initial location that onLocationChanged was called. I wonder if something changed. I'm having this issue too. – Scott Apr 07 '15 at 15:12