onProviderDisabled - need gps in battery saving mode - android

onProviderDisabled is telling me the gps is disabled. I want to be able to get the lat and lon in the wifi gps mode as well as the full gps mode.
I am using this code to start up
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000,
1, this);
It all works with the full GPS on but I need to be able to get the lat and lon in either mode.

You can request updates from more than one provider. Add the following line:
locationManager.requestLocationUpdates(LocationManager. NETWORK_PROVIDER, 3000, 1, this);
The same callback methods, i.e. onLocationChanged(), onProviderDisabled(), etc., will be used to pass the updates from both providers. If you need to know which provider is passing the update, you can use the Location.getProvider() method in onLocationChanged(), and the String provider parameter passed in the others.

Related

how to find latitude and longitude every second without using GPS

i want to find latitude and longitude of user current position and insert in db every second. and drawer a path according to all latitude and longitude of user move on location without using GPS
If you don't want to use GPS you can use services which use WLAN, Bluetooth or CellTower information to determine the position of a device. For Example the Mozilla Location Service.
Only way how you can do it is position based on mobile signal strength https://developer.android.com/guide/topics/location/strategies.html but this methode is not very accurate.
DonĀ“t forget on permission in manifest :
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Then just register a listener and get the updates
LocationManager manager = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1, 10, listener);

How do I find out programmatically which mode of Location is turned on?

I am developing an application in which the user's location is tracked using the GPS provider only (device only mode of Location) . Therefore if the user sets his location priority to Approximate Location using Wifi and Data, the application wont work as intended. Therefore I need to check out the user selection of location mode (High accuracy, Approximate Location or Device only) programmatically and display appropriate dialog boxes. What would be a clean way to achieve this?
You can LocationProvider.getAccuracy:
https://developer.android.com/reference/android/location/LocationProvider.html
From the docs:
int getAccuracy ()
Returns a constant describing horizontal accuracy of this provider. If the provider returns finer grain or exact location, ACCURACY_FINE is returned, otherwise if the location is only approximate then ACCURACY_COARSE is returned.
1, To check the provider mode (gps, network, cellar-id)
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//get currently used providers
locationManager.getProviders(true);
2, show dialog
new AlertDialog.Builder(this).
setTitle("Title").
setMessage("content").
show();
Note that your app will work for both GPS Only mode and also High Accuracy mode, since with both settings GPS is enabled.
In order to check if GPS is enabled, this will do the trick.
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGpsEnabled) {
//show your alert here
}

Better way to query GPS regularly

I have a timer that runs every second. Every second I get the GPS location and do other stuffs.
I am wondering which way is better:
1- Request a single location update and then get the last known location
private void timeout(){
String data[] =new String[DATA_LENGTH];
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
.
.
.
}
2- Start Location listener and then just get the last known location whenever my timer expire
OnCreate(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
private void timeout(){
String data[] =new String[DATA_LENGTH];
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
.
.
.
}
Thank you
PS: Note that battery is not a concern to me as per the requirement of the product
requestSingleUpdate is meant to be single, if you need to query the GPS frequently you should definitely go with option 2.
Keep a global Location object in memory, use it in you other stuff and update it whenever your listener gets an update from the LocationManager.
You can listen for changes via requestLocationUpdates - the code below is a quick-n-dirty example (untested). Remember, you have to have location services turned on to use this.
LocationListener locGPSListener= new LocationListener() {...}
LocationListener locNetworkListener= new LocationListener() {...}
mgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// listens using GPS for location
mgr .requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locGPSListener);
// uses towers for location
mgr .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locNetworkListener);
...
What approach is better, depends on
Androids GPS behaviour and
your Application.
ad 1. if explicitly getting a location delivers a more recent fix, than this is an advantage, because:
ad 2. if your application don't want the android filtering behaviour, and you can filter it yourself better, then this would be better for your app.
Example: (is for ios, but may apply here too:) if I drive with my car to a traffic signal, and do a harsh breaking, then ios still shows 5 km/h speed, although I am standing still. This I call unwanted filtering.
This has all nothing to do with battery: if you get the location via message or if you query it is the same from battery point of view. It smore a software design issue: (events vs. polling)
A difference would only be if GPS is disabled, but disabling GPS makes only sense if it can be disabled for long time.

Android. How to know if location detected is from GPS or Network provider

I'm trying to implement an application which senses position from both GPS and the network provider and shows it on a Google Map. I succeeded in doing this, but I have a question:
I get the provider in the onCreate() of my Activity (it extends LocationListener)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
It works fine, my problem is that when onLocationChange() is called, I should act differently if the provider which called it is GPS or NETWORK. Is there a way to know which?
To be more clear:
When onLocationChanged(Location location) is called, is there a chance to know which provider made the call? I have tried using an if on the provider string but it seems it doesn't work.
Do you need to know the Location provider (GPS, WIFI or Network) or its accuracy?
getAccuracy()
Get the estimated accuracy of this location, in meters.
We define accuracy as the radius of 68% confidence. In other words, if you draw a circle centered at this location's latitude and longitude, and with a radius equal to the accuracy, then there is a 68% probability that the true location is inside the circle.
If you really care about the provider, you could probably use isProviderEnabled().
check that accuracy is <= 30m:
boolean isGPS = (location.getAccuracy() <= 30);
I think you wanna know which provider provided last Location update .....
u have created Provider as a string just use that String in code like
if (provider.matches("GPS")){}
I see several answers, and although they may be useful, none addresses the actual question asked:
When onLocationChanged(Location location) is called, is there a chance to know which provider made the call?
To know whether it was GPS_PROVIDER or NETWORK_PROVIDER the one triggering the onLocationChanged you can use the getProvider() method (or the provider value in Kotlin):
#Override
public void onLocationChanged(Location location){
//obtain a string, 'gps' or 'network', from the location
System.out.println(location.getProvider());
}
Or in Kotlin:
override fun onLocationChanged(location: Location?) {
println(location!!.provider)
}
Doc reference here

Location coordinates does not change when using LocationListener

I am using LocationListener on my application. However, whenever my location changes or onLocationChanged(Location location) method is executed, the coordinates of my location does not change at all. I am riding inside a car and had traveled several meters to kilometers already without my location changing. What could be the problem?
My code:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String bestProvider = locationManager.getBestProvider(new Criteria(), true);
{LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
//location.getLatitude(), location.getLongtitude()
//location coordinates does not change at all everytime onLocationChanged is called even after I already move several meters or kilometers.
}
//...
}
locationManager.requestLocationUpdates(bestProvider, 0, 0, locationListener);
Try this
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
You might be using the network provider. The listener is normally removed in the activty onPause. And you can set the listener in the onResume event of the activity.
Im not sure what is the logic behind finding the best provider by android. But GPS is always considered to be the most accurate compared to the network provider. But there are conditions when getting a location lock by the GPS is not possible. Thats when the network provider comes into use.
I normally create a simple class that calls a function to get location coords. Inside the function, I first request the location using GPS. I wait for 30 seconds for the location, if it is not received, then I request it using the network provider. This way I can always ensure that I am getting the GPS value as my first preference.
My guess is that your location is changing but you are overlooking the decimal part of latitude and longitude. Note the last digits of the decimal part before and after location change.
Try this way in your code
if(isInternetEnabled==true)//check for internet connection here
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
}
else
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
}
Because it happens with me that GPS_PROVIDER doesn't give quick response to location update then NETWORK_PROVIDER.

Categories

Resources