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)
Related
I don't need to be too precise. I want to know location can be an area of the user in one country. I tried using the location service getLastKnownLocation with the update through GPS. If the getLastKnownLocation is unknown, the time it take to get the result is quite a bit of time. I am working on the weather application, I don't need to have the street name. Is there another way of getting the result more faster.
you can get the last known location for more detail read here last Known Location
or you can request a location updates every period of times you set request locaion updates
it's pretty straightforward
I am developing an android application wherein I need the user location updates pretty frequently. Say 2 times a minute.
Earlier I had been using Google Play Service's "Fused location service" but the location updates were not received as requested.
The location updates got stuck for sometime, the interval between updates jumped to 10min or so.Sometimes even if I put my priority to "PRIORITY_HIGH_ACCURACY" the same happened.
I then went back to the old "Location Manager" and when I used the "NETWORK_PROVIDER", I noticed that the location updates got stuck due to this provider. Also the GPS does not get activated immediately, it takes some time. I am trying to build my custom fused location provider. How can I efficiently switch between providers, without getting lags on location updates.
I want to know what are the best practices for getting location updates regularly, all the time, be it either NW, GPS or both. Like it should work for an application where location updates getting stuck cannot be afforded.
Battery drain is not an issue for me right now.I am aware of all the supporting docs that Google provides regarding location access.
Any help would be much appreciated.
Thankyou !
FusedLocationProvider really is the best option for obtaining locations at the moment, because it uses a lot more than just GPS or Network data to obtain location fixes. I have experienced issues regarding intervals being missed as well, but ultimately this is something down to luck depending on availability of GPS, Network, etc. etc.
My favourite usage of FusedLocationProvider so far is in conjunction with the AlarmManager class: Basically, the idea is to start location tracking at intervals specified by the Alarm Manager (this can be set to every 30 seconds such as in your case). Once the interval is hit, the location provider is to obtain a location fix as soon as possible (so in the setInterval method or whatever it's called, the parameter is a 0). This way, you can avoid having to wait another 30 seconds for a new location, instead having the location tracker attempt to provide a location as soon as possible after the interval specified by the Alarm Manager is hit.
By the way, when making custom location tracking wrappers, be careful of using the .getLastKnownLocation() method as it only uses cached locations - you could end up sending the same location to the user every 30 seconds.
Good luck!
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?
In my application uses current location lat,long. which is periodically update in every 5min. and run in background,means when i launch my application a pop up comes and ask for "do you want to use current location" if i press "yes" then service will be run on background of application and use current location lat long,means refreshing in every 5 min,so how to do it.How to make such service.
Please explain in detail as i am new in android .
every help would be appreciated
thanks in advance
Handling location updates in user-friendly and power-efficient way is no trivial task.
I highly recommend you read Reto Meier's A Deep Dive Into Location (and the second part).
Update
There is a simple (and wrong) way to do it: set AlarmManager to wake your service every 5 min which requests the location.
This is wrong for several reasons. To do it right first answer yourself this questions:
Do you need location updates exactly every 5 minutes?
Even if your app is not active?
Even if phone is in sleep mode?
Even if phone is not moving for longer period (e.g. user is sleeping)?
What accuracy do you need? 10m or 300m?
Do you need GPS accuracy on all locations? GPS drains battery in matter of hours.
Do you need updates if user is moving fast? 5 min in a car can mean 10 miles easily.
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.