I need to keep rough track of a users position, but not really in real time. It's sufficient to handle the location updates when the app is started. However, I still need to know where the user was when the app wasn't running.
Is there a way to get the location history in an app?
I don't really want to have a service just polling last known location all the time since that would be a waste of battery power.
However, I still need to know where the user was when the app wasn't running.
That is not possible.
I don't really want to have a service just polling last known location all the time since that would be a waste of battery power.
Then eliminate your requirement for location history. You only get the locations that you request.
Rough Track can mean you get location of the user (lastKnown or Fresh) after every n-hours. Doing this will not require a service, simply a recurring alarm and receiver will do. In the onReceive method of receiver, you can manage a stack of locations in your app.
You will have to reset the alarm though when the device re-boots. I guess this is an add-on, rest should work fine.
you can not get current location without running your app...
the second way is to make background service ..wich you don't wan't to make...
The other way is to run background service using Alarammanager whenever you want after getting location you can stop the service...like you can make call every hour or 2 times per day...
Related
I have an application that is required to log the user's location every couple of seconds, the entire time it is on.
On does not mean in the foreground,or that even the Android's screen is on.
What is the suggested way to accomplish it?
I've heard of background and foreground services, and also saw something about Jobs. I also saw this was possible with a WakeLock.
I am not sure what is the best method of choice.
The need to conserve Battery life is of course an issue.
You should use Service, you can choose that the service wil keep running even after your app was closed.
YET if the device is low on resources it might close your service.
you can use FOREGROUND service if you want to minimize the chance android will close your service.
inside the service use LocationManager / FusedLocation to get the location every X time.
you set it up with LocationListener so everytime it set onLocationChanged you upload to firebase.
Useful links:
Make your app loaction aware
Services
I would like to develop an application - a service - that updates the user location to server, say, every 1 hour.
I understand from the Android docs that I have to use "Android Services" and I have to do the work on the onStartCommand()
But since I need to keep sending these location updates every 1 hour, then I need to run infinite loop inside onStartCommand() ... right?
Now, my questions are:
What will happen if system kill the service, I know my service will get started, but would it call onStartCommand() also?
Is there a better way to implement location update, for example, is it possible that the system call the onStartCommand() method periodically every 1 hour?
AlarmManager, as mentioned by the other posts, is what you're looking for.
However, It's against Android Best Practices to run things "forever, every hour" or so. If you start off by thinking "every hour, every day", you might take actions that would harm the user's battery life.
Turning on gps every hour, waiting for it to acquire a lock, then turning it off, uses a lot more battery life than if you were "smarter" while checking location.
If you do it right, e.g. setInexactRepeating, try to use WiFi location, set listeners for location update, then you can have your cake (long battery life for the user) and eat it too (still get the information you need).
For some good tips about how to keep your app from taking up too much battery (and possibly getting uninstalled), take a look at this video from GOogle IO 2012: http://www.youtube.com/watch?v=PwC1OlJo5VM
EDIT: Take a look at this, too:
http://developer.android.com/reference/android/location/LocationListener.html
You can set a listener that only gets called when the device's location is changed. YMMV, but take a look.
One possible way is to check out the AlarmManager. You can schedule your app to be run on an interval. You can also determine that perhaps I did not change since the last hour and lengthen the delay to extend battery life.
AlarmManager Documentation
Yes... Search for AlarmManager.. That is exactly what you need.
You can schedule intent to be called and therefore start your service every hour.
I have to create an app which runs a service in background and continually update the current location on the server or we can say i have to send my current location at some points. The solutions i have think are following. please look at that and give me some idea.
Running an service in background with boot-starter.
Implementation of alarm manager so service can be started automatically if android kills it.
If my current location is in the radius of the 500 from my points than start to update on server.
and other logic which i have think to prevent continues server update.
I have research on it and found it consumes much battery of phone. So please suggest me how can i optimized it.
There are some ways that can keep your Battery work for a long time though you are using GPS Application.
Turn the GPS function off when not in use, then on when you need a
location.
There are some more ways that you can keep track that GPS doesn't drain much Battery, just read this BLog to get some more idea about it.
I'm looking for the best way to continually monitor the location of an Android device, while using the lowest amount of battery power. My project is basically a phone tracking service, with phone call / sms logging, and location tracking. There is a web interface, which would eventually allow a person to change settings (such as location update interval, so if it was stolen they could track it more effectively), but as standard it should update the location every 30-60 minutes.
I was thinking of using a Service, but would it be better to start a service, request location updates, and then finish? Or some other method?
Cheers
but would it be better to start a service, request location updates, and then finish?
Yes, simply so you do not need to keep a service around all the time. However, it's a bit more complicated than that:
you might never get a fix (e.g., the phone is underground and cannot get GPS signals)
you need to keep the service around until you get a fix or until some timeout occurs
you have to worry about the device falling asleep while all this is going on
I have a LocationPoller component that handles most of that.
I would like to write an app on Android to upload my GPS location to an external website once every ~5 minutes. This needs to have as minimal an impact on battery life as possible, but it also needs to work without any user interaction. (Background: I'm competing in an Ironman triathlon which will take me about 14 hours to complete, and want to broadcast my location in near-real-time but without having to worry about fiddling with my phone.)
So my initial thought is to write a Service which uses LocationManager.requestLocationUpdates() with a minTime of 5 minutes, but will this actually wake the device up every 5 minutes for my service to do its job?
It sounds like I would also need to use AlarmManager.setInexactRepeating() to make sure my service is awake while it completes its task but how does that play with requestLocationUpdates()? Should I instead set minTime=0 on requestLocationUpdates() but then go back to sleep as soon as the next update is obtained?
Any general guidance on how to design this is greatly appreciated. I'm a competent Java programmer & will be using Google Maps on the server to plot my location, but am pretty new to Android development so I'm basically looking for a high-level plan on how to architect the client app.
Your service must be alive all the time you want to receive updates.
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29
You can tell how often you want to be informed of location change with minTime parameter. It does not however decrease battery consumption. GPS is enabled unless you use removeUpdates method no matter how often you want to receive updates.
You can use another approache:enable GPS using method above, read one value, use removeUpdates method, wait 5 minutes and all over again. Delay between enabling and retreiving a location can be between few seconds to few minutes.