Android Programming Tutorials

(Romina) #1
Posts On Location

locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
10000 , 10000 .0f,
onLocationChange);

This refers to an onLocationChange object, which is a LocationListener. We


do not truly care about the updates (though, in principle, our application


could do something with them). Hence, for this tutorial's purpose, all you


need is a stub implementation of LocationListener. Add the following to


Patchy:


private LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location location) {
// required for interface, not used
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};

Finally, since we registered for location updates in onCreate(), we should


remove that request in onDestroy(). Modify onDestroy() in Patchy to look


like this:


@Override
public void onDestroy() {
super.onDestroy();

locMgr.removeUpdates(onLocationChange);
service.removeAccount(listener);
unbindService(svcConn);
}

Step #3: Add "Insert Location" Menu


Now, we need to give the user a way to inject the current location into the


status update being written. The simplest way to do that is by adding a


menu item to Patchy/res/menu/option.xml:


229
Free download pdf