I've been looking everywhere for an example of code that is a Service that update a web server on the users location. this is made almost impossible because of the use of the word 'service' as it is used in the context of location service, for example 'googles location service'. please can anyone point me in the direction of an example where a SERVICE gets the location and does something with it. I can send it to a web service so I'm not asking for that but I just want to know if there is a way of every certain amount of time and distance a service will update the web server?
Look at Android docs about obtaining user location.
You will have to make several decisions:
How accurate do you need your updates to be?
How often do you need them?
This greatly affects device's power consumption. Personally I loathe the apps that prevent full sleep and have GPS constantly turned on - this drains the battery in a couple of hours.
I highly recommend you look at this blog post: A Deep Dive Into Location. There is a link to a great example app.
Related
I'm developing a location aware app. This app will start tracking users when they are in their workday. These are the requirements:
The service should run on a regular basis (every 30 or 45 min).
It won't matter if the service does not trigger at the same basis everytime.
The service needs to upload data to our firestore db.
I doesn't need to be triggered on specific conditions (data is on, phone is charging, etc...)
I need this to run even if the user restarts his phone.
We may need to track the distance traveled by the user. (This is not a requirement per se, but it may be a feature someday)
I know there are a lot of ways to achieve this, but I have not decided which is the option that best fits my scenario. This is what I've thought so far:
Foreground service combined with BroadcastReciever in case the phone is rebooted
Background service using the new jetpack's Workmanager.
Which will the best solution? Do you think there is a better solution than those?
Thanks!
Was thinking create a GPS location tracker so when they are in work premise as well as outside it kinda shows.
Then consider adding the number 5 of the above. Like you said there could be mire awesome solutions than these so lets wait for options.
I'm developing an android app fully based on user location, so I would like to receive the coordinates as frequently as possible. For this case I decided to create a service that will run in the background (looping) and use user GPS and NETWORK providers every 10-15 seconds and after it received a fix, turn them off. Is it a good practice?
When you are convinced that you need user accurate location and you need it to be update then you need it. However you can take a look at this answer to understand preferred manner to implement it (of course with a little change)
And please consider using Google Fused Location :
The Google Location Services API, part of Google Play Services, provides a more powerful, high-level framework that automatically handles location providers, user movement, and location accuracy. It also handles location update scheduling based on power consumption parameters you provide. In most cases, you’ll get better battery performance, as well as more appropriate accuracy, by using the Location Services API.
Refrence
I am developing one tracking application where a user can track other users location continuously and show it in map.For continuous location updates i am using FusedLocationApi and getting location updates correctly. My question is to send these location to other device, is it a good habit to send these location updates to server continuously? If we make connection request to server so frequently then won't it be draining battery fast? Thanks in Advance. Any help is appreciated.
it is not good actually because it consumes a lot part of battery and also network. But as per your requirement it is good. Even i am also working on almost same concept of application but it is okay coz there are no another solutions so keep moving on that...
I have 2 android applications A and B, and both of them are reading gps values based on different parameters. Considering both the apps are running on the device, which of the folllowing approaches would be better?
Both A and B are to be different apps, each one with a component to read from GPS.
To develop a third application with a remote service component to transmit GPS data to both A and B
Would battery usage be minimized by going for the second approach or will the GPS component read once and serve all processes, as in the OS?
Please help
There is a very good explanation given in the Android Developers Website about Location Strategies. I would suggest you to take a look at the code examples on the page.
In both of your approaches i believe second approach is quite better because Turning on/off GPS is a quite expensive operation in terms of battery usage.
GPS’s battery draining behavior is most noticeable during the initial acquisition of the satellite’s navigation message. Acquiring each satellite takes 12 to 30 seconds, but if the full almanac is needed, this can take up to 12 minutes. During all of this, your phone is unable to enter a deep sleep. A-GPS (Assisted GPS) partially solves this, by sending the navigational message to your mobile device over your cellular data network or even Wi-Fi. As the bandwidth of either of these greatly dwarves the 50bps of the GPS satellites, the time spent powering the GPS antenna or avoiding deep sleep is greatly reduced.
Referred from this.
I think the most battery-efficient way would be to poll the GPS location with app A normally, and in app B, use LocationRequests and use setPriority() with PRIORITY_NO_POWER. As mentioned in the docs, PRIORITY_NO_POWER will make app B get updates only when another app gets GPS updates (in this case, app A!!). I haven't tried it, but it should work. It definitely saves you the hassle of an extra app :)
Some more info on Google Play Location Services here and here.
is it the same as OS gPS component will run once to serve all
One GPS serves all.
There is no half GPS saving half the power.
But there are other location providers like cell tower and Wifi locationing which uses less power.
But if you need GPS it is absolutley no difference how many apps uses the GPS service.
If GPS is enabled it uses full power.
For the sake of compatibility and function I would suggest having a third process or program which reads and outputs GPS data, as multiple processes polling data from GPS is less efficient.
It would also be faster to have those two apps read the output of a single GPS tracking app and not needing individual components in each app to do so.
For the sake of power the GPS will use the same level of power regardless, though if it's polled more often due to two applications using it then it may use more - though the amount is likely to be minimal unless there are constant requests for location.
Though this may not be the question it would be most power efficient to have the third application poll GPS at specific intervals and the applications may read from its output rather than search location every time.
Second approach seems to be more appropriate but not sure about battery drainage.It depends upon how you implement it.
Also I would suggest try to use passive providers.Refer following link help it works :)
http://fypandroid.wordpress.com/2011/04/11/298/
I'm designing an application where application in server side need to be continuously aware of user location. I'm thinking to create an Android service that run in the background and continuously query user location and send the result to an application in the backend server. However, I wonder if this is the best approach in terms of battery saving. For example, in iOS, there is something called "Significant-Change" to serve such purpose, does Android have similar thing.
I suggest to create a background Service which just uses the NETWORK_PROVIDER and the PASSIVE_PROVIDER. You can use the minTime and the minDistance parameter to avoid draining the battery. Documentation
And Reto wrote a great post about obtaining users location
http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
Regarding your comment you have to start your service sticky:
Documentation