Friday, May 28, 2010

Refresh our list when we move


Given the location sensitive nature of WamF it makes sense to update the display whenever we move. Do this by asking theLocationManager to trigger a new Intent when our location provider notices we've moved.
    List providers = locationManager.getProviders(); LocationProvider provider = providers.get(0); Intent intent = new Intent(LOCATION_CHANGED); locationManager.requestUpdates(provider, minTime, minDistance, intent);
Intents in Android are like events in traditional event driven programming, so we're triggering a LOCATION_CHANGED event/intent every time we move by a minimum distance after a minimum time. The next step is to create an IntentReceiver (event handler), so create a new internal class that extends IntentReceiver and override the ReceiveIntent event to call our update method.
    public class myIntentReceiver extends IntentReceiver {
      @Override public void onReceiveIntent(Context context, Intent intent) {
        updateList();
      }
    }
We then have our activity listen for a LOCATION_CHANGED intent by registering the event handler and specifying the intent it should be listening for (LOCATION_CHANGED). Do this in the onCreate method or create a new menu option to start/stop the automatic updates.
    filter = new IntentFilter(LOCATION_CHANGED); receiver = new myIntentReceiver(); registerReceiver(receiver, filter);
Keep your phone running light by registering / unregistering the receiver when the activity Pauses and Resumes – there's no point in listening for location changes if we can't see the list.

No comments:

Post a Comment