So it's a well known fact that it takes a while ( about 10-12 seconds ) for the GPS receiver to lock on to the satellites. Using Android LocationListener I want to get notified when the location updates start. For instance:
I have an activity with the following code to instantiate and call the LocationListener
public void startLocationUpdates() {
try{
Common.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new LocListener();
if(Common.locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Common.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
} else if(Common.locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Common.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
}
}catch(Exception e) {
e.printStackTrace();
}
}
Here's the LocationListener onLocationChanged implementation.
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
try{
Common.currentLocation = new String(String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude()));
Log.i("LocListener",Common.currentLocation);
} catch(Exception e) {
e.printStackTrace();
}
}
The Common.currentLocation string gets updated after the locationUpdates start. However, I want to wait in my main activity until this field is updated.
If I put a while loop in my activity to wait for the Common.currentLocation field to be updated, the thread gets suspended and the onLocationChanged method doesn't get called ( the Log messages are not printed ).
What's the best way of getting an update from the onLocationChanged method to a common interface/activity as soon as onLocationChanged gets called and the latitude and longitude values are available ? Any help will be greatly appreciated.
If you need to put the listener in a class for re usability purposes then there a number of methods to provide the information
The important thing to remember is that Android is an event driven system, so instead of "Waiting" for the results by polling you need to create a way that lets the class create an even and then respond to any interested parties of that event.
The simplest thing to do if you don't require re usability is to simply implement the listener in the same activity and do your processing there so for instance
public class yourclass extends Activity implements LocationListener {
...
public void onLocationChanged(Location location) {
...
}
}
The next simplest method is to create an interface in your class with the listener so, this is not error free and from memory but you will get the general idea, in reality this just puts one layer between your class and the event listener but it does isolate you listener into a class that can then easily be used by other activitys, also not I am not doing anything to take into account multiple activity registration de-registration etc.
public mylistenerclass implements LocationListener {
...
public interface LocationEvents {
public abstract void onLocationEvent(Location location);
}
LocationEvents mListener = null;
public void registerForEvents(LocationEvents eventListener, ... any-params) {
... start listening for location stuff
mListener = eventListener;
}
public void unregisterForEvents(LocationEvents eventListener) {
... end listening for location stuff
mListener = null;
}
public void onLocationChanged(Location location) {
...
if (null != mListener)
mListener.onLocationEvent(location);
}
}
public class yourclass extends Activity implements LocationEvents { ...
@Override
public void onResume() {
super.onResume();
registerForEvents(locationEvent, anyparams);
}
@Override
public void onPause() {
super.onPause();
unregisterForEvents(locationEvent);
}
}
You could also use a service, or broadcast intents from your class. I suggest though if your other activities don't need the functionality to just start by implementing the listener in your existing activity and go for there