How to check if LocationManager.NETWORK_PROVIDER
is available?
I enabled in AndroidManifest.xml
but I need to check that in code and if is not available to use GPS_PROVIDER
. Can someone help me ?
How to check if LocationManager.NETWORK_PROVIDER is available?
Use this Part to check whether Network Provider is Enabled or Not :
network_enabled=locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
This will return a boolean values :
true- if enabled.
false - not enabled.
additionally for checking if GPS Provider Enabled :
gps_enabled=locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
This will return a boolean values :
true- if enabled.
false - not enabled.
If you want the user to Enable Network or GPS Provider use this code to redirect user to Settings Page :
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
here is the code for getting the location using Network provider and/or GPS provider
private Location getLocation() {
Location gpslocation = null;
Location networkLocation = null;
if(locMan==null){
locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE);
}
try {
if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);
gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1, gpsListener);
networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
} catch (IllegalArgumentException e) {
//Log.e(ErrorCode.ILLEGALARGUMENTERROR, e.toString());
Log.e("error", e.toString());
}
if(gpslocation==null && networkLocation==null)
return null;
if(gpslocation!=null && networkLocation!=null){
if(gpslocation.getTime() < networkLocation.getTime()){
gpslocation = null;
return networkLocation;
}else{
networkLocation = null;
return gpslocation;
}
}
if (gpslocation == null) {
return networkLocation;
}
if (networkLocation == null) {
return gpslocation;
}
return null;
}
here it will check whether the GPS provider enable then get the location to gpsLocation if also getting the location using Network provider then check the time which was the fast that location object will return
if one of the provider is enable then that location object will be return
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With