do I need to enable gps when using NETWORK_PROVIDER - android

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.

Related

Android Location Manager

There are two providers: GPS_PROVIDER and NETWORK_PROVIDER to get our current location in Android.
Is it possible to disable location service and get the user location purely by WIFI?
I tried to disable the location service but the NETWORK_PROVIDER return false.
Is it possible to disable location service and get the user location
purely by WIFI?
Yes it is possible by adding input to getLocastKnowsLocation as LocationManager.NETWORK_PROVIDER
LocationManager locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location networkLocation = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (networkLocation != null) {
Location bestResult = networkLocation;
}
Yes, you can still get the current location of the device thru wifi. The location data available to an Android device includes the current location of the device. You can use the My Location layer and the My Location button to provide your user with their current position on the map.
One of the unique features of mobile applications is location awareness. The Location API's available in Google services facilitate adding location awareness to your app with automated location tracking, geofencing and activity recognition.
Here's a sample demo app which discuss how to get current location of the device:
https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/MyLocationDemoActivity.java

Android get location from GPS

My situation is like that the user has their tablet with them all day and its gps or location can be turned off whole time to save battery, and then wanna post something using my application which requires them to turn on the location and then upon posting I wanna get the latitude and longitude of the device. Using LocationManager when I get the location its null. Any help would be appreciated.
I'm using the below code to get the location:
LocationManager lm = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
As I've checked around, it seems I have to put a listenere to update the device location so when I get the last known location it wouldn't be null, but how can I do that if the device's location is off before using the application? I wanna get the location where the user is posting using the application.
EDIT: I've changed my code to below, but loc is always null and loc2 always returns location. why is that?
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location loc2 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
and this is the listener:
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
You might want to consider using Network Location as well as GPS Location, as many users just keep the GPS radio off to conserve battery.
With Location Services set in "Battery Saving" mode, the device can get accurate geo-location data using Wi-Fi if available.
If you use Network Location data as well as GPS radio data, you would have a much better chance of getting location data when you need it.
However, if Location Services were disabled completely prior to being turned on in order to post something using your app, then you would need to register a Location Listener to get the first instance of location data.
Take a look at this post to see how to use both Network and GPS Location:
Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location
GPS rarely works indoors. If your users are "visiting shops" then it makes sense the GPS provider will never return a valid location as there is no GPS signal available indoors.
The network provider uses both Wifi and cell towers, so as long as they at least have cell service, you should return a location. That explains why you are always able to retrieve a location update with the network provider.
I would recommend you look into the Fused Provider which is the newest Location API released by Google. This API will automatically handle switching between GPS, network, and passive location listeners for you based on both what settings the user has enabled and which provider gives the best data given the users situation.

How to get the current Location mode of an android phone?

I've taken a look at this page: http://developer.android.com/reference/android/provider/Settings.Secure.html#LOCATION_MODE , but I'm still lost.
I know that the Location mode should return an integer. Battery Saving would be 2, High Accuracy would be 3 and if GPS is off then it should return 0.
I have no problem grabbing the current location...
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String stlongitude = Double.toString(longitude);
String stlatitude = Double.toString(latitude);
But when the phone's location mode is set to Device Only then I have to use LocationManager's GPS_PROVIDER. Which is not as accurate as the NETWORK_PROVIDER. I would like to do an if, else or a case statement that allows me to get the current location mode and if it's set to High Accuracy or Battery saving, use the NETWORK_PROVIDER but if it's set to Device Only use GPS_PROVIDER.
What you are trying to do with Settings.Secure and LOCATION_MODE is the right way to get the enabled providers if you are only interested in targeting devices that are KitKat or above (API 19). But, more than likely, you actually want your app to work with devices that have earlier versions of Android.
Here's code that gets the last location using the least power-hungry provider that the user has enabled:
LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGpsEnabled && !isNetworkEnabled) {
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} else {
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
NETWORK_PROVIDER is not as accurate as GPS,
The first thing to note is that NETWORK_PROVIDER is not as accurate as GPS, according to the developer docs, network prodiver uses
NETWORK_PROVIDER
This provider determines location based on availability of cell tower
and WiFi access points. Results are retrieved by means of a network
lookup.
The most accurate provider you can use when retrieving the users, location is the GPS, which is defined by the Andorid Developer docs as:
GPS_PROVIDER
This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission ACCESS_FINE_LOCATION.
Manipulating the provider
With that in mind, the following hints will enable you to manoeuvre the logic any way you wish:
1) To set the location manager to recieve updates using best provider the device is capable of using, you can call
/* Getting the name of the BEST provider available */
provider = locationManager.getBestProvider(criteria, true);
/* Getting Current Location using the best provider available */
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 300000, 0, this);
2) If you which to force network provider, set your location manager to ask for network provider update like this:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, this);
3) Similarly, if you which to force GPS use:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
I think if you are looking for LOCATION_MODE, not a provider, you should check out this question and response: Change Location Mode to High Accuracy Programmatically Android
Because even if all your providers are enabled, the location mode could mean that it would fail to get a good location if you are in a building and the mode is LOCATION_MODE_SENSORS_ONLY.

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.

How to get latitude and longitude in network location provider in device android java application

I am implementing gps provider getting latitude and longitude from device i am already get the information in device i can implemented network provider that is the problem how can implemented using network provider
The code is basically the same. Obtain a provider (gps, network, etc.), call getLastKnownLocation. Additionally you can request location updates for this provider. But you can't test this using the emulator, you'll need a phone.
LocationManager manager = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE);
manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// additionally (you have to implement LocationListener)
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0.0f, new LocationListener() ...)

Categories

Resources