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.
Related
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'm fresh with Android. I face some ambitious university project, so I hope you'll tell me if my idea about following app's implementation is correct and efficient:
Business scenario:
WebService consumes informations about user's sms (sendt/received) and GPS/"network provided" position
GPS/"network provided" position's should be sendt to WebService every 1 minute (if it's value changed)
sms update should be sendt to WebService immediately
My implementation idea...
I'm going to use AlarmManager prepare Intent matching BroadcastReceiver (and schedule it with 1 minute interval). Then I'll start WakefullIntentService in onReceive() method. This will feed my WebService in the background. That would work for sending data over HTTP in the background.
... and doubts:
How about updating GPS/"network provided" position data in the background? Should I start some additional service and use LocationListener within it? There would be no sense for using AlarmManager then - I could feed my WebService from this location-monitoring service.
But as I read here: Diamonds Are Forever. Services Are Not. it's not a good practise to play with never-ending services. As I understand it would not work correctly when my phone is sleeping.
First things first, polling the GPS every minute is going to KILL your users battery. So I'd rethink about that portion of your application.
Your alarm manager idea is fine in theory with the exception of using a Wakeful IntentService. If you're waking up the phone every minute (or more often), you're once again going to kill the battery. What you should do is when you get the broadcast, save what ever data contains to disk (probably in a sqlite database). When the device fully wakes up (this is a broadcast you can recieve), then flush the content to your webservice.
Do not use the alarm manager. It is unnecessary for what you want to do.
Use a background service that implements LocationListener. You can specify how often you want to check for location updates and even how much distance must change in order to consider it a new location. This is done in the requestLocationUpdates method.
Here is some code to get you started:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 1, this);
This will try to get a new location every 60 seconds and only if the distance has changed by 1 meter. That is the location listener will only fire if the conditions above are met.
Hope this helps.
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 am writing an application that has to send to the server its GPS location every X seconds, and I was wondering if the right place to have this running was inside the Application Class since it has to keep sending messages even if you change activities.
Right now I initialized inside the activity and then have a different thread with a timer send the latest location to the backend.
Am I correct? I ask mainly because I have had complains that the GPS sometimes gets stuck, but when they launch another application with a GPS it starts working again in my program.
[EDIT: the application is for a company that will be using the app alone in a Galaxy Tab]
Thanks
A simple way: Create a Service and register it to listen to LocationManager updates. If you want to send data even when phone goes to sleep then take a look at WAKE LOCK.
However this will use GPS and network and will drain battery. People don't like such apps - I know I wouldn't use it.
To make things more user-friendly:
Use passive location provider. This leeches location data when other apps use Gps. Use Gps directly only when you dont get data for a longer period.
Cache location data. Register with sync manager to update data when other app also update data. Use DownloadManager to upload data files: DM is smart, it automatically retries on error, even if device is restarted. If files are big, then set it to only upload over wifi.
I highly recommend watching Reto Meier's Android Protips video http://www.youtube.com/watch?v=twmuBbC_oB8
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.