I have successfully been getting GPS data through the registerLocationListener() and onLocationChanged() methods. The only problem with this is that the speed reading of my app freezes if there is no more GPS data (e.g. when I go indoors, enter a tunnel, etc). The behavior I want for my app is that the user is somehow notified that the speed reading is probably not accurate due to a lack of fresh data (set speed to zero, blink the speed reading, etc).
How can I do that? I though of checking periodically whether the GPS unit was detecting any satellites, but I'm not sure how to force periodic checks.
Maybe you could just start a periodic timer. If timer sees that last GPS fix is old it displays notification. Application should remember when the last fix was.
Related
I have a big problem with an Android application that I developed.
The purpose of the application (for business) is to track the position of the device continuously (24 hours on 24) detecting a GPS track on a regular interval, which will then be synchronized to the server to the unleashing of certain events.
Of course, over time the device in and out of buildings, acquiring and losing the GPS signal continuously.
Often the device is not used and remains in office for several hours inside the company headquarters without GPS signal. During the course of time the Android system continues to provide me constantly getLastKnowLocation
My problem is that after some time that the system is running, sometimes two or three days, sometimes more days, my app starts to receive from the system always the same coordinates, regardless of who is in the open or at closed. From what I understand the Android operating system no longer seems able to update his coordinates and It will always return the same getLastKnowLocation indefinitely.
My App therefore becomes useless.
You know how I can fix this?
Is there any process that Android can restart in order for the system to wake up. In My App I will acquire any permission, except for root permissions.
For now the only control that I put, and that if the system always gives me the same identity for a number of seconds I call the method requestLocationUpdates of the location manager again.
But I need something more robust, to give me a better guarantee of operation. I'd like to be sure as possible that the systems try really to get updated coordinates.
The only thing I can think of is that maybe I can ask the system to restart some process so that the Location Manager, is fully reset. Can I do this? What is the process to be killed?
Can you think of any other solution or you found yourselves in this issue?
Thank you.
Is there a way to find out that the GPS signal is lost? I am not talking about whether GPS Module is enabled/disabled. Suppose that GPS Module is enabled; but fix is lost?
This How can I check the current status of the GPS receiver? link is not useful; since you cannot use that solution in the case which user is stationary for a certain period of time("LocationListener" stops listening to location updates; after you receive couple of updates upon first fix).
More than that, unlike on gingerbread, on my Nexus 4(which has Jelly Bean 4.2.2) "onStatusChanged" method of "LocationListener" is not called anymore! I dont know whether it depends on being stationary or not; yet it doesn't mean anything for me again.
On the other hand, My API is compiled via API 10, however in that release, no action called "GPS_FIX_CHANGE" had been declared in "LocationManager" class at that time.
What should i do to have my app notified of signal lost and gained events?
Kind Regards
Android and iphone both have a stand still filter enabled, which as you described, leads to the situation that you don't get a location update when standing still.
I would just ignore that, and use the Location.accuracy value for each new location received. If that values exceeds 30m then it is bad GPS.
While standing you could decide to do nothing.
Theoretically there is a solution for your task, but with drawbacks:
When you don't get a new location for some time, you can disable and reenable Location Service every 5 seconds. Then in case of good GPS you get a new coordinate, or none if there is no GPS signal available.
I've encountered this same issue and came to this conclusion: when the GPS signal is lost the listener will return the last value or 0.0 (in some situations). If the last 2 values are equal to the last 5th decimal then the signal is lost. In practice the GPS will never give you two consecutive coordinates with the same value. It's close to impossible, so just consider this case if(last_coords == new_coords) -> GPS signal lost.
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.
In my app, I need fresh gps co-ordinate's in all Activity's. I don't want to initialize the gps function in all my activity's or don't want to keep static references(as for as my view it is not best practice).
What I need is.
1) initialize the gps function once,and use it all over the app.
is there any way to solve my problem
You could create an always running service that tracks the GPS data. Every time onPause is called, it begins 1 minute counter (example) until it disables the GPS monitor. And then onResume would cancel that countdown allowing the GPS data to be maintained or else start listening for GPS locations.
This approach would allow your app to have the same GPS data while not draining the battery excessively (since the GPS would be turned off when app is not in use).
The minute long countdown is only selected since the user might exit your app momentarily then open it again.
You might have a look at the PASSIVE_PROVIDER constant of the LocationManager class : http://developer.android.com/reference/android/location/LocationManager.html#PASSIVE_PROVIDER
It lets you receive locations that would be retrieved via other providers (GPS, network...) at other places in your application without too much additionnal work on the device.
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...