After I disable location services on my phone and then enable them getLastKnownLocation() returns null although the provider it used is enabled.
Does anyone know how to solve this problem?
Also, if I open Google maps app and then return to my app it seems to solve the problem.
Code:
private Location getLastKnownLocation() {
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
boolean enabled = mLocationManager.isProviderEnabled(provider);
#SuppressLint("MissingPermission")
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
Log.d("DebuggingLocation", "Skipped " + provider + " " + String.valueOf(enabled));
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = l;
}
}
return bestLocation;
}
Logs:
D/DebuggingLocation: Skipped passive true
D/DebuggingLocation: Skipped gps true
D/DebuggingLocation: Skipped network true
Problem solved by requesting location updates for passive, gps and network providers.
Related
I am using LocationManager to get the last known location. I found out that it returns null unless the GPS is turned on.
here is my code
mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
#SuppressLint("MissingPermission") Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
is there any way to get last known location without needing to turn GPS on?
I want to get current GPS location when button is clicked. I have written this code (given below) but this gives NullPointerException for location object that is Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);. please tell what is problem with my code?
Code:
#Override
public void onClick(View v) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}
String cur_loc = "latitude = " + location.getLatitude() + "\n" + location.getLongitude(); //this is line 54 in my code
Toast.makeText(ctx, cur_loc, Toast.LENGTH_LONG).show();
}
AndroidManifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
getLastKnownLocation (String provider) may well return null. see Documentation :
Returns a Location indicating the data from the last known location
fix obtained from the given provider.
This can be done without starting the provider. Note that this
location could be out-of-date, for example if the device was turned
off and moved to another location.
If the provider is currently disabled, null is returned.
try this method:
private Location getLastKnownLocation() {
mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
Now use it as:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location myLocation = getLastKnownLocation();
is android stores last location, if so is it possible to get the last location when GPS/Mobile Data/Wifi is off.
The last known location of the device provides a handy base from which to start, ensuring that the app has a known location before starting the periodic location updates.
check this link .its very useful to us.
http://www.androidwarriors.com/2015/10/fused-location-provider-in-android.html
You could use locationmanager
LocationManager locationManager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);
// default
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);//get bese location provider
if (provider != null && !provider.equals(""))
{
//check for permission
if ( ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
String Latitude = "" + location.getLatitude();
String Longitude = "" + location.getLongitude();
}
}
}
You need permission
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
public Location getLastKnownLocation() {
LocationManager mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null
|| l.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = l;
}
}
if (bestLocation == null) {
return null;
}
return bestLocation;
}
Why this function returns incorrect Location?? The mistake is about 8-9 kilometers
Because getLastKnownLocation doesn't return the current location. It returns the last location it figured out via that provider. That position could be an hour old- it will still return it, if it has it. If you want to get an accurate location you need to turn on the location provider by calling requestUpdates or requestSingleUpdate, then wait for the onLocationChanged function to be called at least once. Anything else may be a stale location.
It's because you're using getLastKnownLocation() : location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
getLastKnownLocation(String Provider) : Returns a Location indicating the data from the last known location fix obtained from the given provider.
This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.
From : http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29
I have the following code:
SosServiceListener mlistener = new SosServiceListener(this);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// getting network status
boolean isNetworkEnabled = manager.
isProviderEnabled(LocationManager.NETWORK_PROVIDER);
String provider;
if (isNetworkEnabled) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
provider = LocationManager.GPS_PROVIDER;
}
manager.requestSingleUpdate(provider, mlistener, null);
manager.getLastKnownLocation(provider);
but the SosServiceListener is never called :( any idea why?
String provider;
if (isNetworkEnabled) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
provider = LocationManager.GPS_PROVIDER;
}
is GPS enabled on the device, with Satellite option set in Security section of mobile settings? because you are assuming that GPS_PROVIDER is enabled, just because NETWORK_PROVIDER is disabled? which is not accurate ...
also manager.getLastKnownLocation(provider); will return Location object directly and will not result a call to callback methods in the listenet...
so you need to use either one of them...