0

I have a problem coming along with Android Fused Location API. I have an IntentListener service that I need to receive locations periodically. I create and connect GoogleApiClient successfully, then I request to receive location updates through PendingIntent, but every time I call GeofencingEvent.getTriggeringLocation the return value is always null.

Here's the code:

private void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

private void startLocationListener(Context context) {
    if (!isGooglePlayServicesAvailable(context)) {
        Log.d(TAG, "No Google Play Services.");
        return;
    }

    createLocationRequest();
    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mGoogleApiClient.connect();
    }

private void startLocationUpdates(Context context) {
    Intent i = new Intent(context, IntentListener.class);
    PendingIntent pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

    com.google.android.gms.common.api.PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, pi);
    Log.d(TAG, "Location update started ..............: ");
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "INTEEENT");
    String action = intent.getAction();

    if(Actions.ACTION_START_INTENT_LISTENER.equals(action)) {
        init(this);
    }
    else {
        GeofencingEvent event = GeofencingEvent.fromIntent(intent);
        if(event == null) Log.d(TAG, "Null event");
        if(event.getTriggeringLocation() == null) Log.d(TAG, "null location"); // **<-- PROBLEM HERE**
        if (event == null || event.getTriggeringLocation() == null) {
            return;
        }

        Location location = event.getTriggeringLocation();
        Log.d(TAG, "LocationC: " + location.getLatitude() + ", " + location.getLongitude());
    }
}

And here's the manifest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.srn.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:enabled="true" android:name="com.srn.app.IntentListener"></service>
</application>

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

I'm running the app in x86 emulator with Android 5.1 installed.

Any idea what could be the problem?

Thanks.

Davita
  • 8,928
  • 14
  • 67
  • 119

1 Answers1

1

The Geofencing API must be specifically registered via GeofencingApi.addGeofences(). Assuming you actually want to receive the location information from the requestLocationUpdates() call you are doing, you should use LocationResult.extractResult() in place of GeofencingEvent.fromIntent().

An alternative for older versions of Google Play services is to use the key KEY_LOCATION_CHANGED to extract just a single location from the Intent:

Location location = intent.getParcelableExtra(
    LocationServices.FusedLocationProviderApi.KEY_LOCATION_CHANGED);
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thanks Ian. I'll try this evening when I get home :) – Davita Jun 19 '15 at 08:52
  • Strange. There is no LocationResult class in com.google.android.gms.location package. Maybe they changed something..? – Davita Jun 19 '15 at 16:22
  • Maybe you're using an older version of Google Play services? The latest is 7.5 – ianhanniballake Jun 19 '15 at 16:23
  • I have revision 25 installed. I'm not sure which version it is, but revision 24 corresponds to 7.3. so it probably is 7.5 because there's no updates in SDK Manager. – Davita Jun 19 '15 at 16:26
  • If you're using Android Studio / Gradle, you should check your `build.gradle` file - just because it is installed doesn't mean your app automatically updates its dependency to the latest version. – ianhanniballake Jun 19 '15 at 16:27
  • That was it. Thanks for the tip :). I upgraded Google Play services in my project, but the emulator has an older version 6.7.74 even though I have latest version installed in avd manager. Any idea how to upgrade google play services in emulator? Thanks again :) – Davita Jun 19 '15 at 16:39
  • Looks like I'm stuck with 6.7.74 until google updates android images. Is there any way to acquire location with this version of play services? – Davita Jun 19 '15 at 17:11
  • Added an alternate method to retrieve the location from the intent which I believe will work on Google Play services 6.7.74. – ianhanniballake Jun 19 '15 at 17:31
  • It worked. :) Thank you very much, I was already desperete :)) BTW, is it possible to receive more than one location in a single hit? And if so, would you mind telling me how do I enumerate in all those locations? Thanks again :) – Davita Jun 19 '15 at 17:56
  • Not with the Google Play services 6.5 SDK - batching location updates was introduced in [Google Play services 7.3](http://android-developers.blogspot.com/2015/04/theres-lot-to-explore-with-google-play.html) which is probably also when you get to start using `LocationResult.extractResult()` which returns a list of locations you can enumerate through. – ianhanniballake Jun 19 '15 at 18:02