user456234
user456234

Reputation: 1

android location update problem

I have an activity that needs to display an alert to user when location is updated, but i need first to get current location (update last known location) and then start a location update listener to display alert in this activity.
Basically what i need is to display an alert if location changed from time activity was created.
If i use getLastKnownLocation when activity is created, it might be outdated, then the requestLocationUpdate listener is called a few seconds from activity creation time.
How can i do this?


Thanks in advance,

Upvotes: 0

Views: 2450

Answers (2)

Mitesh Jain
Mitesh Jain

Reputation: 673

Check Blow Link.. May be it's help you... In that u have to pass sach argument so that Location manager can easily Update Your Location and your code is also right to get Location ....

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28long,%20float,%20android.location.Criteria,%20android.app.PendingIntent%29

Upvotes: 1

Patrick Boos
Patrick Boos

Reputation: 7029

Getting last known location (quickly)

LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

from ( http://developer.android.com/guide/topics/location/obtaining-user-location.html#FastFix )

Listen for location updates: http://developer.android.com/guide/topics/location/obtaining-user-location.html#Updates

This is the functionality you can use. But know that last location might be pretty old and inaccurate. Depends on when a application was trying to get the location the last time. But it normally should be somewhere in the area you are.

If that is not good enough, than you will have to do something like this:

  1. start listening to updates
  2. save the answers for the first 1-3 times or so. (didn't test what would be best. maybe even higher). or maybe for 1 minute or 5 minutes (gps can take a long time if you need the location exactly)
  3. after that you do not save the location updates anymore but check if it moved.

I hope this helps.

Upvotes: 3

Related Questions