I have a background thread that needs to get the GPS location every 1 hour.
What would be the best way to do this?
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000*60*60,0, this);
have the thread sleep for a hour and then register for updates and after receiving the location will
removeUpdates(...)
And what is the difference between LocationManager.NETWORK_PROVIDER and LocationManager.GPS_PROVIDER.
Instead of using LocationManager use Location API . It provides much better accuracy and eats less power and its faster at getting the location.
Your second option from point of GPS consumption is the best one. But instead letting the thread to sleep for an hour I would use a timer with period = 1 hour. If you cannot garuantee that your app will stay active for that hour, you want to store the last reveived location time. Once your app wakes up next time you check the system time stamp and compare it to your last location time on permanent storage. If the hour was exceeded you immeadetly demand a location, and later continue with timers. If the hour has not yet passed, the you calculate the time difference and start a timer with initial delay of that difference, and period of 1hour.
But thi sanswr is not dealing how to keep your app alive, it tells you from point of power consumption that is is better to enable GPS only for that short moment you need it.
Getting a GPS location will need from 20-40s.
You should set highest accuracy. (GPS)
After enabling GPS and receiving a valid location, check the horicontal accuracy.
Leave GPS enabled until the accuracy reach the desired threshold (e.g <15m).
Then disable GPS.
With that solution GPS needs only power for about a minute every hour, which is negligible low.
You can use Criteria for that, customize it according to your need.
A class indicating the application criteria for selecting a location provider. Providers maybe ordered according to accuracy, power usage, ability to report altitude, speed, and bearing, and monetary cost.
code sample :
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Have a look at how to use locationmanager , how to specify Criteria and how getBestProvider method works for reference
Related
Currently we use LocationManager to get GPS updates every few seconds, we use it for high accuracy updates.
Besides, we also want to get location coordinates every 5 minutes irrespective of GPS state (turned on/off), we really ignore accuracy of location in this scenario. We want to use FusedLocationProvider for this operation.
Can both locationmanager and fusedlocationprovider co-exist in the same app; if so, are there any reliability issues?
I know with the Android and the Location API's it's possible to receive periodic updates of latitude and longitude through an interval of time using the "setInterval" method and other methods on the "LocationRequest" class, but what I'm wondering is, what would be a good way to get latitude and longitude updates via GPS based off of a change in distance compared to the previous location without having it on all the time and not wasting battery (preferably a solution that is energy efficient).
For example, say I start at one place and then 2 minutes later I'm 5km from the distance that I originally started at. At this point the GPS would detect the change in location and update the latitude and longitude on the device.
If anyone could provide some insight or code samples on how to do this, that'd be great!
Thanks
You can based your position attribute update on how far you are from the previous location, but you can't based GPS frequency update on that, simply because you need a location request to find out that you are far enough.
If you want an energy efficient solution, you can reduce the rate at which new update appear. You can also reduce the window duration in which you listen for position (position may be less accurate).
A lot of information can be found here, specially in Adjusting the model to save battery and data exchange and Deciding when to start listening for updates chapters
You can get the location change if you specify the distance you want to get updates in the requestLocationUpdate parameter.
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
Here is example:
LocationManager yourLoc = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
yourLoc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 2, onLocationChange);
Here you will receive location changes after every 2 meters you have travelled, irrespective of the time you spent at a particular location.
Hope that answers your question!!!
I am trying to make an accurate location based app but I am a little bit confused on how the requestlocationupdates works.
If I put the requestlocationUpdates(LocationManager.GPS_PROVIDER, , ); as a location provider and I test my app. I see that if I haven't enabled the gps it takes updates from the network and when its enabled it takes it from the gps. What is the point of setting both network and gps providers to send updates if they switch on their own?
How does the third parameter of requestlocationUpdates work? I mean it says that it changes in the distance that I set but how it can detects that I have moved?
You can try with following code
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
where Criteria is a class indicating the application criteria for selecting a location provider. Providers maybe ordered according to accuracy, power usage, ability to report altitude, speed, and bearing, and monetary cost.
then
locationManager.requestLocationUpdates(provider,0, 0, new MyLocationListener());
Where second and third parameter are use to so that you can control the frequency at which your listener receives updates with the second and third parameter—the second is the minimum time interval between notifications and the third is the minimum change in distance between notifications—setting both to zero requests location notifications as frequently as possible.
keep in mind, once you requestLocationUpdates(...) means onLocationChanged(...) will be triggered when provider specified is enabled and you will get the latest coordinate in onLocationChanged method. There is a default location aka Cache location, or LastKnownLocation which that system will be use before detecting any new location.
Of course system will treated that you're moving when your current location is different than your previous location (cache location).
In my app the GPS takes too much time to get the location.
How can I use GPS from GPS_SATELLITES and GPS _NETWORK_PROVIDERS simultaneously in the same context and get the value of the recent GPS?
Keep track of a Location object that is your current location. Request location updates from both
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
LOCATION_UPDATE_FREQUENCY, LOCATION_UPDATE_MIN_DISTANCE,
gpsListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_UPDATE_FREQUENCY,
LOCATION_UPDATE_MIN_DISTANCE, networkListener);
then when you get a response in onLocationChanged of either listener, either simply replace the Location variable you stored or replace it only if it is X seconds more recent, X% more accurate, X meters distant from your last reading, etc.
You can also use LocationManager.getAllProviders(), then call LocationManager.getLastKnownLocation(provider) to retrieve the last location each available provider found the last time it was called. This way, if another app used the GPS a minute ago, you can just go ahead and trust that they user hasn't made it that far away without hunting for satellites all over again.
When getting GPS location on a fixed interval (say per hour), does this drain the battery every hour, every minute or only when either actually getting the location each hour or updating the location. I'm eager to know what goes on in the background.
Here is what this reference says
"Background services should be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended. "
So I'm hoping that when the location is changed, or after minTime, the GPS radios are on, and after getting location, the GPS radios are off
Can anyone confirm this to be true?
According to reference we can decide that expensive battery using happens when GPS engine try to get new location and we can control freqency of this using method public void requestLocationUpdates (long minTime, float minDistance, Criteria criteria, PendingIntent intent)
Battery consumed is at its peak at the time when location manager tries to pinpoint your location(you can observe GPS icon blinking on status bar at that time) and this happens once in an hour in your case.. So GPS is automatically turned to "not available" state once you get a location until your next hour is reached..
And the line "In particular, values under 60000ms are not recommended" is said because it takes some time for GPS to point out your location, typically a minute maybe depending on your satellite strength.. So if your minTime is set to less than a minute(60000ms) its like your GPS is almost on for all the time..
Read here for more details