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.
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)
Using google maps api to create a route while the app is open.
right now, I am updating every second. is this fine? how often does the google maps app update for example? Does it kill the battery/hog the CPU if I have the location updated every 1 second?
thank you
Once a second is fine.
You cannot save battery by updating once in 5 seconds.
To save battery you have to request in a much lower frequence, like once in 20 minutes.
It depends of your application how accurate the tracking must be and how often you need a location.
I think it depends on how precise your app should be. It is true, that having a short delay drains battery, but if you want to track user's movement precisely, you have no choice. Try to approximately check GPS interval of some similar tracking apps, but I think that many of them just keep GPS turned on.
Play with it and you will find the ideal configuration for your desired app. :-)
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!
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.
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.