getTriggeringLocation always returns null

981 views Asked by At

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.

1

There are 1 answers

10
ianhanniballake On BEST ANSWER

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);