locationManager.getBestProvider - android

I'm working on an application which uses location.
My problem:
When I'm looking for the best provider, I only get the "network".
I know why but I don't know how to improve this.
In the locations settings, when I check "parameter -> location" and "security settings -> Use wireless network", the LocationManager.getBestProvider() returns only network. When it is not checked, and the GPS is active, getBestProvider returns the GPS.
What I want to do is:
When both options are checked, how to use / detect the GPS, instead of the network, as the location provider.

You have to first check whether GPS is on or not. If it is on then get the location from GPS, if not get the location from network.
To check the gps status use :
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

Related

do I need to enable gps when using NETWORK_PROVIDER

I've written an android application that needs the user location, it was working fine when I used gps provider, but I don't want to use GPS because it uses a lot of battery, here is my code
LocationManager locaionManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5*1000,0, new MyLocaionListener());
Location loco = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("enabled=" ,"" + locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
This code returns a null Location Object, and the logCat shows enabled=false when the location service is disabled although I'm using NETWORK_PROVIDER But when I enable the location service from the phone settings I get enabled=true and I get my current location.
So my question is:
does network provider also uses the phone GPS? and how do GPS and NETWORK providers internally works.

Location detection on KitKat

up until now, when using the LocationClient in order to obtain the user's last known location, I had no problems, testing on Jelly Bean for example.
I got a KitKat phone to test on, and I found that there is a DEFAULT setting in Settings -> Location, that is called "Device only" and it solely relies on GPS to start, connect and obtain location. This takes A WHILE. Also, when Im indoors it might get no location whatsoever.
If I go to Settings, and switch to "High accuracy" I go back to my app and BAM location is right there.
Question is:
How do I detect which of those three location settings is set on KitKat and ask the user to change it (or directly change it myself)
The three settings are:
-device only
-battery save mode
-high accuracy
As far as I know, you can individually determine which of the services are enabled/disabled using the LocationManager class.
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
So all you have to do would be to determine which of them are enabled, and act accordingly. If either of them are disabled, you could ask the user to turn them on.
if(!isGPSEnabled) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
if(!isNetworkEnabled) {
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
EDIT :
You could use the LocationManager class to just get your settings right and then use LocationClient to actually gather location information.

Android google maps v2 isproviderEnabled(LocationManager.NETWORK_PROVIDER,0,0,this) returns false always

boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
The first one returns true when I have the GPS on, but the second one returns false even though im connected to my wifi. On the location settings i have enabled both gps and wifi for location determinations.
We need more information like api level.
Check this thread, this must solve your problem. And if you want to get location, just use the google_play_service locatioClient.
Network Connection is closed but location provider is NetworkProvider
After some thorough search on the phone's settings, my Network Location app was disabled for some unknown reason. Wifi was turned on, and both gps and network were chosen for location settings. So after turning it back on and a phone restart, I was finally asked to allow the droid to store my location on the phone

Android Fall back to ACCURACY_COARSE if GPS in not available

I am using the Criteria object to get the best provider like so
final boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(isGpsEnabled) {
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
crit.setCostAllowed(true);
crit.setPowerRequirement(Criteria.NO_REQUIREMENT);
crit.setSpeedRequirement(false);
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 600000, 0, new myLocationListener());
}
On my phone (Android 4.1) I have both "Use GPS Satellites" and "Use Wireless Networks" options enabled. Now, the above code works great when I am outdoors and it gives me the GPS location.
But, when I am indoors it does not revert to the "network" provider. It just keeps trying to get the location via GPS and never get its (I wait 1 minute or so)
When I change the code of Criteria to use Criteria.ACCURACY_COARSE then it uses "network" provider.
How do I get it to first try the GPS (because it is enabled) and because we are indoor it will not be able to connect to a satellite to then fall back to using the "network". I can't get that working easily. I state again, GPS is enabled but no access to satellites so want it to get network location instead.
Thanks.
The solution was create the location manager and attach 2 listeners to receive updates. One for GPS and another for NETWORK. You set it to receive updates fairly quickly (or depending on your own case, I just needed to get the location) You then create a method that compares the location of the GPS and Network and find which one is more accurate. You do this at most 3 times to get on average which one is returning the most accurate position and then you stop the location updates.

Android mock location providers not cleaned from LocationManager

I'm having an issue with mock location providers. I have an app that needs to get accurate valid locations, and I don't want the user to be able to spoof them. So I have a check for the mock locations enabled setting, which works fine.
However there is a problem with the location provider. If you enable mock locations, set a location with a gps faker app, then disable mock locations and all other location providers, the location manager still has a reference to the mock location provider. So although mock locations are disabled, the location manager tells me that gps is enabled, and serves mocked location data.
I know that you can clear test providers from the location manager, but mock locations must be enabled to do this
Anyone ever come across this, or have a work around?
Think I found a way to get an accurate reading whether GPS/Network provider are enabled or not:
ContentResolver contentResolver = context.getContentResolver();
boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
boolean networkEnabled = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.NETWORK_PROVIDER);
Are you sure you've called :
removeUpdates()
removeTestProvider()
setTestProviderEnabled() with false
clearTestProviderEnabled() ?
Look at how LocationManagerTest does this.
// disable mocking.
Settings.Secure.putString(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION, "0");

Categories

Resources