The correct answer from Destil handles the case when at least one provider returns a valid location for getLastKnownLocation() .
However, I also saw Glass return null for getLastKnownLocation() for all providers (specifically in XE16).
In this case, your only option is to register a LocationListener and wait for the new location to be updated.
For example, in the context of obtaining a location when creating a new action, it looks like this:
public class MyActivity extends Activity implements LocationListener { ... LocationManager mLocationManager; Location mLastKnownLocation; @Override protected void onCreate(Bundle savedInstanceState) {
Then you need to handle the LocationListener callbacks:
@Override public void onLocationChanged(Location location) { if (mLastKnownLocation == null) {
It can be a little tricky to switch to an asynchronous model to avoid blocking the UI thread while waiting for an update, and you may need to move some part of your application logic.
Sean barbeau
source share