In blackberry, we use a timeout to get the location, so that if it doesnt retun location in that much time period, we get to know. But in Android, there is no concept of timeout, can anyone please tell the alternative, that we can find out that after this much time there is no location update from GPS.
You can use two threads for this timeout. One thread tries to get the GPS fix and the other (timeout) thread, once it reaches the timeout specified, removes the registration of your application to receive location updates from the location listener.
Related
I'm currently working on an Android app that use location to operate. Imagine that when you run app, it will display all near-by hotel.
After reading the Internet, I applied Google Fused Location to my app but some problem occurred and I have no idea how to solve it.
Here are my approach:
When SplashActivity start, I run the LocationService to create location request and request location update. I use HIGH_ACCURACY option and use both GPS and Network Provider.
In onLocationChanged() I call Geocode Intent Service to parse lat lng to address.
When Geocode Intent Service complete, I receive the result thanks to ResultReceiver (I did the exact same way like Google tutorial) and send a Broadcast to SplashActivity.
When SplashActivity receive broadcast, I start my MainActivity.
Please let me know if my approach is a correct way to use FusedLocationService, this is the 1st time I make location-based app.
Sometime, onLocationChanged() took so long to response (about 3 minutes, sometime forever). I notice that this problem occurred only on Android OS 5, 6, 7 (I do request permissions) but again sometime it really fast (1-2 secs). This problem is killing me, I have no idea how to solve this.
I'm thinking about apply last known location to solve the above problem, but I wonder if there is a way to set a time-out for requestLocationUpdate(). And my app need user current location to display data, so I don't think last known location is a good idea. Anyway, it's good to hear your opinion about this.
Thanks for reading.
Firstly, considering a location is fresh or old could depend on your scenario. I meant, a ten minutes old location maybe useful for weather app, but doesn't sound good for tracking intent.
IMHO, the whole process seem a bit bad, you can give chance to receive location for user even after splash screen
Anyways, i have a scenario for you
Define a timeout value, for instance 10 seconds
Make location request with immediately values (set interval and fastest interval 0)
If you receive location, remove location updates, stop timeout and use location (for geocoder)
We couldn't receive location and timeout finished, refer to last known location
If the last known location is nice for us(mentioned in first paragraph) use location (for geocoder)
I am having trouble finding an approach to wait for a GPS fix in my Android application. I am running a service to record user location and want to wait for a GPS fix before I do so. Can anyone suggest a method for approaching this?
Just get a locationManager and request location update for GPS. Do not call getLastKnownLocation. When you get the onLocationChanged() that is your GPS fixed.
This is indeed one of the critical things.
You will have the problem, that either you could have an not accurate fix, or you are waiting for an acurate one, and the user does not get an position feedback in your app.
This much depends on your app.
You can wait until the location.getAccuarcy() is under 30m, or the first one that have a speed over x km/h, for application swhere you want to record movement.
To show on a map, you want to take the first you get.
There is no universal soultion for this.
After few hours of testing outside of house, when i came back to my house i found that GPS is enabled but was not getting location fixes inside the building.Hence its onLocationChanged method couldn't get called.
Problem: How to know that GPS is not getting any location fixes as device continue to sense your location in "trying mode".By trying mode i mean the situation where it is not coming to any result even after 20 to 30 minutes still it declared it self as enabled (blinking in status bar).
How one could know that the GPS doesn't get location so switch to another provider like Network_Provider.
In short i want to get my device to conclude something that GPS can find location fix for sure or you have to take location by another means.
I hope at least someone can give me idea about how to deal with that.
The link below has an awesome tutorial, of how to get the location from GPS and/or Network.
It uses a timer task, which analyzes if there is a GPS location in a specific period of time, assume 20 seconds. If not, it will return the location from Network as the current location. If there is a location from GPS, then it will compare which update is new (latest), and return that.
What is the simplest and most robust way to get the user's current location on Android?
I want to know what are the best practices for Location-based Android app's architecture/workflow?
My current code uses several Activity and one backing Service, and several AsyncTask.
I start my Service as soon as my app is launched, I do all the HTTP callings and parsings in my Service. And I also wrote a subclass of AsyncTask to obtain user's location. I run the AsyncTask everytime I need to update user's location. The AyncTask calls LocationManager.requestLocationUpdates() and asks to get locations as fast as possible. My strategy for this is:
1. At first, I getLastKnownLocation for both GPS and network, and compare them using the method on http://developer.android.com/guide/topics/location/obtaining-user-location.html#BestEstimate.
2. When 3 GPS locations and 5 network locations is obtained, or one or both of GPS and network didn't respond in 1 minute, I stop the task.
3. I return the best estimate I have.
the Locating AsyncTask is run in the Service, and I set an AlarmService for 5 minutes to send my service an Intent to check whether I need to update user's location. My min interval between two AsyncTask is 10 minutes. User is able to request Location updates manually by simply pressing a button.
Above is how I implement the Location service into my app.
I need to know whether my practice is appropriate. If not what is wrong? If yes, is there anything that can be improved?
I think youy pretty much got it. I have very similar solution where I want "in and out" as soon as I can.
I implemented check for "accuracy" into my algoritm. I give service 1 minute from start to get BEST possible location or I will exit even earlier if I get 25m or better accuracy fix.
Also, I don't use AsyncTask for location itself - Service doesn't block thread, it get's and processes callbacks from LocationManager so I don't see why you want to do AsyncTask.
When I'm done with obtaining location and about to exit - then I call async task to process Http post to the server.
As far as interval - I give user options of 5/15/30/1hr and 1/2day. I use inexact alarm for this - supposedly better on battery.
This is not an answer per say , but here are some additional points. Reto Meier , in his Pro Android tips in Google IO suggested not using wi-fi if the battery was low.
This can also be extended to location. Let the user know his battery is too weak & the app wont be using GPS & network provider , instead it will be using the last known location which may be inaccurate.
You have not mentioned it, so I am going on the worst case scenario that you can not checking if the location providers are enabled. You need to check for the same and have an alert asking the user to enable location service if all are providers are disabled.
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.