Android: How to periodically check current location without draining the battery - android

I have a background service which works periodically by timer.scheduleAtFixedRate.
It wakes up every amount of time (let's say 60 seconds for example) and checks for the location.
The location is checked by locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 5, listener); and the actual location is collected from the listener's onLocationChanged.
Now, when the phone is outside and GPS reception is good, this works fine.
But, if the phone is inside, the GPS is almost always active - looking for a signal, and the battery is drained rapidly.
I created another thread using a Handler and a Runnable in order to conrol the GPS active time accurately:
I used locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); and locManager.removeUpdates(listener); so I can open and close the GPS as I want.
In this case, I can open the GPS for the exact amount of time, but found out that it doesn't lock in an area with good reception even after 10 seconds. So here I'm draining the battery again...
I'm using API level 7, hence I cannot use locationManager.requestSingleUpdate.
I have two questions:
Is there any way to optimize this process?
Will upgrading to API level 9 (and use locationManager.requestSingleUpdate) improve the process significantly? I mean, does it worth upgrading?

How about NETWORK_PROVIDER explained here?
You could have a system that if you cannot find a location in limited set of time, which means you are inside or GPS reception is not good enough, you can use the network location instead. Since that uses less battery, it would at least be a workaround or alternative. But the presicion would be lower.

Related

Can't get HIGH_ACCURACY background locations in Android 8+

Android 8 only allows background location updates a few times per hour. This isn't a problem since I only request them about once an hour and only if the device isn't STILL and there arn't already passive updates doing the job. So maybe a few times a day. No detectable battery use on Android 7.
However, I do have an accuracy requirement and I discard locations with horizontal accuracy worse than about 1000 feet (Basically cell tower locations). The problem is that the network location comes almost instantly, and then the app is throttled and never receives the next update which might come from the GPS. Typically I would wait a minute to get a location with the desired accuarcy and then give up. On Android 8, even if I wait an hour I just get the network location every 20 minutes or so because of the throttling.
I can't find any LocationRequest setting for miniumum accuracy which would solve the problem entirely. PRIORITY_HIGH_ACCURACY absolutley does NOT do this. It is only a "hint". setSmallestDisplacement() doesn't help because it doesn't affect the first update. Batching updates helps on rare occasions in my testing but not with any reliability.
Further, waiting in PRIORITY_HIGH_ACCURACY for any length of time is not reasonable from a user perspective since the location indicator will be active on the device the whole time, even if the app is being throttled.
Does anyone have suggestions on how I could solve this problem? Perhaps Android does not intend for PRIORITY_HIGH_ACCURACY to be used in the background at all, although I can find nothing about this in the documentation.
If you need GPS, just use the GPS provider. Don't screw around with the Criteria, request what you really need.

getting the highest GPS update rate from the GPS Hardware in my Android

I got to program Android from other platforms I used with GPS . on the other platfroms I had access to the GPS HW (I had a low level GPS driver) and by that I could get GPS updates 5 times per second and even more
Now I work with Samsung Galaxy S2 (which it is clear to me that its GPS is very strong and can supply several updates per second
Currently when I set the GPS provider to supply updates in minimum distance and minimum time like this:
mlocManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, mlocListener
);
I get 1 update per second (or 800ms as smallest gap between two updates) which is too low for my application. Is there a way to force the GPS HW to report more updates somehow ? I guess it is not exposed to the user but is there still some way to access the registers of the GPS HW somehow ?
Currently you can't get updates faster than once per second in most of the phone.This is the hardware gps limit.
Usually in most of the phones,it is at 1hz.
It all depends on hardware and scenario where you are using ( Indoor,Outdoor)
But again, You could check using
LocationRequest setFastestInterval (long millis)
This allows your application to passively acquire locations at a rate faster than it actively acquires locations, saving power.
Unlike setInterval(long), this parameter is exact. Your application will never receive updates faster than this value.
This method sets the fastest rate in milliseconds at which your app can handle location updates. You need to set this rate because other apps also affect the rate at which updates are sent. Location Services sends out updates at the fastest rate that any app requested by calling LocationRequest.setInterval().
If this rate is faster than your app can handle, you may encounter problems with UI flicker or data overflow.

Android requestLocationUpdates minimumtime + minimumdistance don't really work

locationmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationlistener);
I understand that this line of code is supposed to update the device location as frequently as possible right? Specifically, every 0miliseconds and 0metres (minimum).
Okay, the word 'minimum' is key here but still, why sometimes the location is not updated for maybe as long as 30+ seconds? I have timed the actual time between one update and the next, while on WiFi, and it took 38 seconds even though I had 0 and 0 in the code. I even tried it with 1000ms (1second) but still doesn't update as frequent.
How do I make this update more frequently? Why does it respond this way?
Thank you!
The short answer is: you can't (make it update more frequently).
The documentation indicates that you will get "updates as frequently as possible" if you specify min_distance and min_time as zero. So what you are seeing is "as frequently as possible". Usually you won't get an update unless something has changed, so if you aren't moving you won't necessarily get an update. Also, since you are using Network location and not GPS, the accuracy of locations isn't that good so you shouldn't expect updates that frequently.
Also, each hardware vendor implements the location providers differently. So the frequency of updates is determined by the hardware vendor's implementation.

Android location updates interval

I am experimenting with Androids location updates. The requestLocationUpdates is responsible for providing the updates. With the following code:
locationManager.requestLocationUpdates(provider, 300000, 10, this);
I am only supposed to receive updates 5 minutes and 10 meters apart. But the updates just keep coming in seconds apart and even when I am sitting still.
GPS is the provider I am using.
I need to space the updates out. What can I try?
The 10m is too small - I would increase that. GPS accuracy isn't great, and so every time it senses a small difference you will get another location. I'd bump it up to 100m and I expect you will then get a sensible number of locations coming through.
If you do want it more specific, then you'll need to handle the volume as more accurate means more volume.
Hers what i'd do:
First of all I check what providers are enabled. Some may be disabled on the device, some may be disabled in application manifest.
If any provider is available I start location listeners and timeout timer. It's 20 seconds in my example, may not be enough for GPS so you can enlarge it.
If I get update from location listener I use the provided value. I stop listeners and timer.
If I don't get any updates and timer elapses I have to use last known values.
I grab last known values from available providers and choose the most recent of them.
The 10m is too small for a GPS reading, try 100m. You'll also have issues with power saving mode, and app battery optimisation on most Android phones.
Android 10 and above has very strict background location updates, and may kill apps that run in the background. Later versions will also remove apps that have not be used recently.

GPS update interval is faster with good signal?

I'm trying to limit my program to take location updates every 10 seconds instead of constant updates in order to reduce battery drain. This works fine when i'm debugging indoors and the signal is weak (i.e. the GPS icon flashes), but if the phone gets a proper fix (i.e. the GPS icon is static) the update interval increases to roughly a second.
I know that the code mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, updateInterval*1000, 0, this); will not force the GPS to take updates exactly at the given interval, but in my opinion it shouldn't depend on the strength of the signal and fluctuate that much.
Any ideas?
UPDATE: see comment
I suspect that the GPS radio works in a manner where either it's connected to GPS satellites or it's not. When it's connected, the Android SDK sends you updates as frequently as they're available from the GPS hardware. When it doesn't have a full GPS connection it falls back to sending AGPS updates according to what you've requested.
If you only want updates every 10 seconds, you should save the last received Location's time value in your listener, and when you receive a new Location check its time against the old value; ignore it if it's too frequent (or do something smarter like checking the accuracy and replacing the old value, etc).
Maybe it works slower because you are debugging, but not because your signal is weak! Try to make tests with disconnected debugger indoors...

Categories

Resources