1

I have a ListView that is bound to a data adapter that displays distances of fixed landmarks after I create and load my location object with the device's geoloc (lat/long). The problem is, on average, the code below takes 3-7 seconds to load, the longer process is the call "requestLocationUpdates()" which sometimes can take minutes to load if there is no NETWORK_SERVICE and the GPS_SERVICE becomes the primary, making my Activity ListView blank

Is the OnCreate() Activity method the best place for requestLocationUpdates() to be called?

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locationListener = new GeoLocationListener();

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(false);
    criteria.setSpeedRequired(false);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    String provider = locationManager.getBestProvider(criteria, true);        

    locationManager.requestLocationUpdates(provider,
        0, //300000, // TODO: Default to Check Every 5 Minutes OR
            0, //500,   // TODO: Default to Check if device moves more than 500 meters
            locationListener); 
CampbellGolf
  • 812
  • 12
  • 27

3 Answers3

2

Read the answers here. They should help you best.

I would put requestLocationUpdates() into onResume() and removeUpdates(this);into onPause(). This way your app will stop updated locations when it is not active. If you need the gps all the time then its ok to put requestLocationUpdates into onCreate() though.

Also to speed up the gps fix you can use the last know location:

    mLocationProvider = mLocationManager.getBestProvider(new Criteria(), false);
    Location location = mLocationManager.getLastKnownLocation(mLocationProvider);

Additionally you can display a spinner instead of your list until you get a location in onLocationChanged(Location location)

Community
  • 1
  • 1
Renard
  • 6,909
  • 2
  • 27
  • 23
1

Use getLastKnownLocation first to display the latest known location in the list view to avoid showing empty list then call requestLocationUpdates, I can't see any problem reagrding calling it in the onCreate method.

Mahdi Hijazi
  • 4,424
  • 3
  • 24
  • 29
  • 1
    You're right.. I ran into this article pending help: "You may not care about all available location providers but just want to get the best one available. To do this, we specify our criteria for "best". For example, do we require altitude and bearing, allow cost, etc. We can now ask the location manager for the last known location, for the specific provider. This is helpful to quickly place you close to last location, while waiting for updates to come in." http://marakana.com/forums/android/examples/42.html – CampbellGolf Apr 16 '12 at 19:38
0

You should not depend on the location updates to fill your listview. Try getting the last known locations by calling .getLastKnownLocation() and then maybe show an indicator that your app is busy requesting a more accurate location.

Read A deep dive into location

xorgate
  • 2,244
  • 1
  • 24
  • 36
  • Mahdi beat you to the punch, but you're both right, and I commented on his response with the code solution. thanks for the help! – CampbellGolf Apr 16 '12 at 19:41