I have an application that is a service and what I want to 开发者_高级运维do is have the service kick on when the user is in motion, going faster then 10 mph (or whatever value I want).
What I don't want to do is keep polling the GPS because if I do that, the battery will drain fast.
What are some options that I can use to do this? Realistic options that will not drain the battery would be great! Thank for the help.
One way to minimize having to constantly be listening for location updates (GPS) is you could toggle location updates on/off based on feedback you get from the accelerometer. For example, when you break a set threshold of movement with the accelerometer, you could start listening for location updates and check to see if the device is moving fast enough. If not, you could turn off location updates.
There are optimizations here like "if the accelerometer threshold was broken less than 30 seconds ago, ignore" so that you do not end up thrashing with location updates being turned on and of.
Here is a developer example of Accelerometer Usage.
One idea without GPS would be creating something like a step-counter:
- The user has the phone in his trouser pocket.
- Every time the phone detects a shake, the user made one step.
You would then need to know his "step-length" and count how many steps he does in one minute. Then, you multiply the count of steps with the length of one step and you have meters/minute
1 meter / minute = 0.0372822715 mph
This would also work with using meters/second:
1 meter / second = 2.23693629 mph
Of course, this only works if the user is actually running/going/riding his bike and it's not very accurate. If you want to check the speed of him traveling by car or boat, this won't work.
One way you could do it is by Inertial Navigation, but this it is doubtful that the built-in sensors are accurate enough to measure anything significant.
Another way would be to reference distances between your device and Wireless Access Points, I was talking to a guy who said this method would be accurate down to a micrometer. However, it presents its own problems. Gets complicated quickly, especially if you're not moving in a straight line, plus you have to have some WAPs.
I think GPS is the most viable option.
精彩评论