Mahmoud Abou-Eita
Mahmoud Abou-Eita

Reputation: 943

Android Location info withought onLocationChanged

I'm writing a simple service that listens for Location changes. In my onStart method I have the following

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationListener = new MyLocationListener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

Then how the service reacts to the changes is handled in the onLocationChanged(Location loc) method.

However I want to get the initial location when the service starts. Suppose there hasn't been any changes in the location info, I'd like to use location info at least once when the service starts, even if the location hasn't changed. How to do so?

Upvotes: 0

Views: 161

Answers (1)

Vipul
Vipul

Reputation: 28093

Use getLastKnownLocation here.

Following snippet will help you.

LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);

Upvotes: 1

Related Questions