mardi 5 mai 2015

Android how to get information from intent when using requestLocationUpdates()

I am trying to get my location in background, I've read that you need a Pending Intent for this.

It seems that my Location Receiver does receive the Intent every 10 seconds as I specified in the Location Request.

My problem is that my location in the Location Receiver is always null...

thanks for any help :)

Here is my code :

   /**
     * Runs when a GoogleApiClient object successfully connects.
     */
    @Override
    public void onConnected(Bundle connectionHint) {

        Intent mIntent = new Intent(context, LocationReceiver.class);
        PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 42, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mPendingIntent);
    }

Here is a part of my xml file :

        <receiver
            android:name=".LocationReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="com.myapp.databerries.LOCATION_READY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

And here is my LocationReciever file :

public class LocationReceiver extends BroadcastReceiver {
    public LocationReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle b = intent.getExtras();
        Location loc = (Location)b.get(android.location.LocationManager.KEY_LOCATION_CHANGED);
        if (loc != null)
            Log.d("hello", "location = " + loc.toString());
        else
            Log.d("hello", "location = null");
    }
}

Aucun commentaire:

Enregistrer un commentaire