In an android app, I have written code to get geo data through GPS or Network, which one is enable get the data through that. Here is my code,
public boolean getLocation(Context context, LocationResult result)
{
boolean gps_enabled=false;
LocationManager lm;
boolean network_enabled=false;
this.context = context;
locationResult=result;
Toast.makeText( context,"getLocation()",Toast.LENGTH_SHORT).show();
if(lm == null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try
{
gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
catch(Exception ex){}
try
{
network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
catch(Exception ex){}
if(!gps_enabled && !network_enabled)
{
Toast.makeText(context, "GPS & Network disabled", Toast.LENGTH_SHORT).show();
return false;
}
}
Using LocationManager check whether GPS & NetWork is on or not.
If GPS is in ON, in boolean value gps_enabled assigned “true”.
But in network, I have activated 2G Internet access in HTC desire android device, and Switched ON in Mobile network.
The boolean variable network_enabled assigned to “false” even network in On state.
Are we want to change in settings anything in device? or any mistake in my code?
That depends on availability:
NETWORK_PROVIDER: Name of the network location provider. This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.
GPS_PROVIDER : Name of the GPS location provider. This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission android.permission.ACCESS_FINE_LOCATION.
See this link: http://developer.android.com/reference/android/location/LocationManager.html#getBestProvider%28android.location.Criteria,%20boolean%29