I'm trying to receive background location update from 3rd party apps through
mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, MIN_TIME_SHORT, MIN_DISTANCE, mLocationListener);
But it turns out that the PASSIVE_PROVIDER only gives location updates from NETWORK_PROVIDER. Is there a way to also receive updates from GPS_PROVIDER or it is not supported?
PASSIVE_PROVIDER means if other application fires the location search, our application will also given the location info.
Is there a way to also receive updates from GPS_PROVIDER or it is not supported?
To get the position from GPS_Provider you have to use this snippet :
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
where lm is your LocationManager. Don't forget to set uses-permission
PS: you can use both Network_Provider and GPS_Provider
Related
I have these two lines of code ready to be used in the section of my app where the user's location is requested:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener);
The problem is that, when testing in real device, I have noticed that, using GPS_provider does not work very well when being in certain places like inside of a building....the app is stuck waiting to acquire GPS coordinates which take a lot of time or just never come unless I have open-sky conditions.
The question would be: how do I do to still use GPS_providerby default but, if gps coordinates take more than XXXX seconds to be acquired, then switch to Network_providerand acquire position from Network.
Small edit: is it maybe any way to check GPS signal strength before using GPS or Network provider?
LocationListener has a function onStatusChanged(). You have to override this function to check GPS_PROVIDER's status and accordingly take necessary action , which in your case is switching to NETWORK_PROVIDER.
Hope this answers your query
I'm having a problem with android where even after the user turns their location service on, the LocationManager still can't find the users location.
Before I launch the activity that needs to use the user's location I call the following:
//Menu Activity
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER){
Intent nearby = new Intent(mActivityContext, NearbyActivity.class);
startActivity(nearby);
}else{
alertNoGps();
}
Once GPS is turned on the nearby activity can be launched, but the LocationManager still cannot find the users last known location. My relevant code for this is:
//Nearby Activity
try{
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
...
}else{
Toast.makeText ...
}
} catch(SecurityException e){
e.printStackTrace();
}
The phone I'm currently testing on is running Android Marshmallow 6.0.1. If the GPS is already turned on before the app is run through android studio then I have no problem getting the last known location. It's only when the GPS is turned off before I run the app and turn it on while the app is running that I have problems. What about LocationManger is causing this?
Edit: Also in the manifest I have both Fine and Coarse location permissions. I do not know if this is an issue with API 23 and above because of the extra permission checks required, but I have to imagine they are still handled by the Security Exception.
There are two different ways to retrieve the location, one is based on network and another is based on GPS services. Certainly, location based on GPS services would always be more accurate.
According to the documentation, it might take some time to receive the location using the GPS services. In this case, the last known location can be used to get the recently available location by using getLastKnownLocation() method.
Here, to get the last available location, you should change
LocationManager.GPS_PROVIDER to LocationManager.NETWORK_PROVIDER.
And if you wish to receive the accurate, real time location based on GPS services, I recommend you to use FusedLocationApi.
You can find it here
https://developer.android.com/training/location/receive-location-updates.html
Cheers..!!
This is exactly how it should work. Android clears the last known location for a provider when it is disabled, and will return null until a new point is polled for that provider.
In your situation, I would recommend that if you get null location point for last known location with GPS, just request for a single update.
I found out that similar to Pablo's answer, getLastKnownLocation() will be cleared and because it is a rather casual function can return null quite often. If GPS coordinates are more demanded, then you should be more forceful when trying to get GPS coordinates and use something like requestLocationUpdates() or Google's FusedLocationApi rather than getLastKnownLocation().
So now I am using FusedLocationApi with a GoogleApiClient I built using:
client = new GoogleApiClient.Builder(mActivityContext)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
After the client is connected I find the GPS location in the onConnected() method override which looks like:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = LocationServices.FusedLocationApi.getLastLocation(client);
This seems to be much more reliable in finding GPS coordinates.
I develop app, which in service get location and send it to server.
Service in background get location every 5 min(for example).
Battery quickly dies, when GPS and Wi-Fi uses..
So, how can I save battery life ?
Thanks for help in advance!
Small question:
Am I right to do request location updates every 5min code below?
And is it correctly use requestLocationUpdates with NETWORK_PROVIDER and next with GPS_PROVIDER ? That necessary, when GPS not find signal, but Wi-Fi find signal and give coordinates.
I do this:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, locationListener);
First of all, you do have to realize that these functions that you need to use are probably one of the most demanding in terms of power. I've compiled a few things that might help you consume less power:
First of all see if you really need the accuracy GPS_PROVIDER. If not choose NETWORK_PROVIDER.
From what I see you set the requestLocationUpdates callback method to be called 2 times, by setting the NETWORK_PROVIDER and the GPS_PROVIDER. You do not need to use them both. Plus take a look at the parameters that you pass on the requestLocationUpdates method:
`public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)`
According to the documentation the minDistance is minimum distance between location updates, in meters and you have set it in 0. That means that the device will constantly acquire the users location.
I am able to set a location proximity alert with addProximityAlert and receive the alerts with a broadcast receiver.
But how will I be able to identify the location proximity alert is fired from a location provided by a GPS_PROVIDER and not a NETWORK_PROVIDER. I just want to make sure that the alerts are fired from an accurate location.
I have exactly the opposite problem - I would not want it to use the GPS provider due to the battery drain. But it seems there is no currently known solution. One thing I might be investigating is setting the location radius higher - hoping that a radius of 1000m would automatically not use GPS for example. In my current testing, it seems a radius of 250m will definitely trigger GPS location. At leas
I read on various sources that ProximityAlerts use BOTH Network and GPS provider, so I think that it really depends on the specified radius.
In my test, if I have GPS enabled but set a 1000m radius, it fires the alert in a way that makes me think it's using the Network provider, because I'm sure I'm over the 1000 radius (thus the position detected by the device could only be approximated)
It is not recommended to use GPS provider as it ideals when phone's screen is off, and battery drain issues, but still if you want to then use following:
LocationListener ll;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
locationManager.addProximityAlert(latitude, longitude, radius, -1, proximityIntent);
//you have to define latitude,longitude,radius etc parameters yourself
you can change "GPS_PROVIDER" to "NETWORK_PROVIDER" in order to use network provided location
I have found plenty of examples of creating a location listener where you supply a particular provider, like so:
LocationManager lm =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
However, I don't need a precise location, but I'll take the best available. Is there a way to create a listener that works for the best available at the given time without creating a listener for each provider?
e.g. Use GPS if it is available, if not, Network, etc. Also, this is a widget, so I don't want to check what is available, then create a listener for that, since the widget will be up for a long duration and may live through enabling/disabling providers.
Thanks for the help
Have you considered using the PASSIVE_PROVIDER? That combined with getLastKnownLocation() might get you most of the way to what you want... You would call getLastKnownLocation() initially to establish a location if one was already known to the device, then the PASSIVE_PROVIDER would listen for updates from any location provider (i.e., location requests triggered from any other application on the device).
There would still be the possible scenario of no other application requesting location, so you'd likely want to trigger at least a single initial location fix using GPS_PROVIDER or NETWORK_PROVIDER.