Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Get recent location only, not getLastKnownLocation?

I am developing an Android application in which current and precise location is key. After GPS fix is detected, it moves to the next activity.

The problem is that I tested it outdoors and found that sometimes it just uses a location from a previous fix.

So what I am asking; is there any other logic to get location from LocationManager other than by getLastKnownLocation? I haven't yet found an example that wouldn't look something like

    Location location = locationManager.getLastKnownLocation(provider);

Thanx!

like image 1000
dhafnar Avatar asked Jul 09 '13 13:07

dhafnar


People also ask

What is the difference between getlastlocation () and getcurrentlocation ()?

getLastLocation () gets a location estimate more quickly and minimizes battery usage that can be attributed to your app. However, the location information might be out of date, if no other clients have actively used location recently. getCurrentLocation () gets a fresher, more accurate location more consistently.

How to get the last known location of an app?

Get the last known location 1 Set up Google Play services. To access the fused location provider, your app's development project must include Google Play services. 2 Specify app permissions. ... 3 Create location services client. ... 4 Get the last known location. ... 5 Maintain a current best estimate. ...

How to get the current location of any Android device?

There are two ways to get the current location of any Android device: Question: Which one is efficient and why? Answer: Fused Location Provider because it optimizes the device’s use of battery power. Before moving any of the above methods we will have to take location permission.

How to get the correct location from the GPS in Android?

If not then ask first. Step 1: Creating an instance of LocationManager in the context of LOCATION_SERVICE. Step 2: Check that if the GPS and Network are available or not and if both are available then we use one with greater accuracy. Step 3: Creating an instance of LocationListener (package: android.location) for both (GPS and Network).


2 Answers

Here's the sample code from the official guide (with little modifications):

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
         makeUseOfNewLocation(location);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

In here, you just request updates from the GPS, and as soon as the location is received, you'll get a callback call.

like image 161
Malcolm Avatar answered Sep 22 '22 09:09

Malcolm


You can use LocationListener and then call locationManager.requestLocationUpdates() passing your own listener implementation.

like image 43
Pozzo Apps Avatar answered Sep 18 '22 09:09

Pozzo Apps