Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: How to get location information from intent bundle extras when using LocationManager.requestLocationUpdates()
    text
    copied!<p>I am trying to use Android's LocationManager requestLocationUpdates. Everything is working until I try to extract the actual location object that in my broadcast receiver. Do I need to specifically define the "extras" to my custom intent so that the Android LocationManager before I pass it to requestLocationUpdates so it knows how to add it into the intent, or will it create the extras-bundle regardless when it passes the fired intent to the broadcast receiver?</p> <p>My code looks like this:</p> <pre><code>Intent intent = new Intent("com.myapp.swarm.LOCATION_READY"); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0); //Register for broadcast intents int minTime = 5000; int minDistance = 0; lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, pendingIntent); </code></pre> <p>I have a broadcast receiver that is defined in the manifesto as:</p> <pre><code>&lt;receiver android:name=".LocationReceiver"&gt; &lt;intent-filter&gt; &lt;action android:name="com.myapp.swarm.LOCATION_READY" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; </code></pre> <p>And the broadcast receiver class as:</p> <pre><code>public class LocationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //Do this when the system sends the intent Bundle b = intent.getExtras(); Location loc = (Location)b.get("KEY_LOCATION_CHANGED"); Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show(); } } </code></pre> <p>My "loc" object is coming up null.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload