I am using LocationManager to get a single location fix:
public class MyActivity extends Activity {
private LocationManager lm;
private ProgressDialog myDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, ll);
myDialog = ProgressDialog.show(this, null , "Determining Your Location", true);
}
class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
...
lm.removeUpdates(this);
myDialog.dismiss();
}
}
}
}
As this stands listening for a location could potentially go on forever if a location fix cannot be found. I want to add some robustness to my code by ceasing to listening for a location after 60 seconds, and displaying an error to the user to saying that their location could not be determined.
How can I do this?
One way would be to use a Handler and use postDelayed after 60 sec to stop the listener.
Handler postDelayed