Reputation: 2291
In my application, I tried to search the coarse location of the android device using the networkProvider. I only use the networkProvider in my location manager, but it won't work if I don't turn on the GPS sensor on.
Is the networkprovider supposed to give a coarse location no matter the GPS sensor is on or off?
Here's my code.
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
if(isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
Log.e("Network","suc");
else
Log.e("Network", "fail");
if (!isNetworkEnabled) {
} else {
this.isGetLocation = true;
if (isNetworkEnabled) {
Log.e("GpsInfo", "isNetworkEnabled true");
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
Log.e("GpsInfoClass", "Location manager not NULL");
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lon = location.getLongitude();
Log.e("GpsInfoClass", lon + ", " + lat );
}else{
Log.e("GpsInfoClass", "Location NULL");
}
}else{
Log.e("GpsInfo", "Location Manager is null");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
In my log, it prints Network(tag) fail(log).
What this mean is that,
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
always returns false, if the GPS sensor is off. Why is this happening? What am I missing about getting coarse locations?
Upvotes: 1
Views: 966
Reputation: 43322
Is the networkprovider supposed to give a coarse location no matter the GPS sensor is on or off?
No, the GPS radio has nothing to do with getting Network Location.
However, you need to have your Location Settings set to either Power Saving
or High Accuracy
.
Using this code to show a Toast with the current enabled providers:
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Toast.makeText(context, "GPS Enabled: " + isGpsEnabled + " Network Location Enabled: " + isNetworkEnabled, Toast.LENGTH_LONG).show();
Here is what it shows for GPS only
and Power Saving
:
So, you can see that this call:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Returns false for the GPS Only setting, since that disables Network Location. And, it returns true for the Power Saving setting.
It would also return true for High Accuracy as well, since that enables both GPS and Network Location.
Upvotes: 2
Reputation: 382
GPS sensor only needed for exact location. If you create a Criteria
and give it to locationManager, system automatically selects best provider and tries get coordinates.
Example;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria locationMode = new Criteria();
locationMode.setBearingRequired(false);
locationMode.setSpeedRequired(true);
locationMode.setAccuracy(Criteria.ACCURACY_MEDIUM);
locationManager.requestSingleUpdate(locationMode, new LocationListener() {@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}, null);
}
Upvotes: 1