I would like to detect when the phone is in motion, but not all kind of motion.开发者_StackOverflow For example picking up or waving the phone should not trigger.
I would like ideally to run a code when the phone/person is in > "walking" state. What options I have?
you can use a combination of the accelerometer and the digital compass, in phones that have them, to determine a speed and direction.
If all you need to do is determine if the person is walking, all you need is the accelerometer. Just process its output for foot steps.
There are plenty of tutorials on the web for detecting foot steps with an accelerometer.
You can also get your location from Wifi-Networks and Cell Towers. All location providers in Android are subclasses of android.location.LocationProvider. That's probably a good place to start. I don't know that either of those would be best for you, as their "range" can be several hundreds of feet wide.
Android accelerometer provides us the feature to know the acceleration using SensorEvent class. So use the object of the class, and handle onSensorChanged(), to determine the movement in the device.
x = sensorEvent.values[0]; States the acceleration in x direction.
So what you may be interested in is, find acceleration in x,y and z directions, and try to calculate the Average and standard deviation for the last 10 such samples. Working on the standard deviation will surely get you to the right point! If it goes to 0, it means device is still. If SD>0.5 for more than 15sec's or so..it means device is continously moving! Lemme know if you need more help!
Cheers, Nithin
精彩评论