how to calculate phone's movement in the vertical direction from rest?
If you integrate the acceleration twice you get position but the error is horrible. It is useless in practice. Here is an explanation why (Google Tech Talk) at 23:20. I highly recommend this video.
Now, you do not need anything accurate and that is a different story. The linear acceleration is available after sensor fusion, as described in the video. See Sensor.TYPE_LINEAR_ACCELERATION
at SensorEvent. I would first try a high-pass filter to detect sudden increase in the linear acceleration along the vertical axis.
I have no idea whether it is good for your application.
Distance moved by Accelerometer
You get position by integrating the linear acceleration twice but the error is horrible. It is useless in practice.
Here is an explanation why (Google Tech Talk) at 23:20. I highly recommend this video.
However the gyro mouse might work for your application, see between 37:00-38:25 in the video.
Similar questions:
track small movements of iphone with no GPS
What is the real world accuracy of phone accelerometers when used for positioning?
how to calculate phone's movement in the vertical direction from rest?
iOS: Movement Precision in 3D Space
How to use Accelerometer to measure distance for Android Application Development
How can I find distance traveled with a gyroscope and accelerometer?
How to capture finger movement direction in android phone?
Implement onTouchEvent(), and calculate dx and dy by where the user presses down and lifts up. You can use these values to figure out the direction of the move.
float x1, x2, y1, y2, dx, dy;
String direction;
switch(event.getAction()) {
case(MotionEvent.ACTION_DOWN):
x1 = event.getX();
y1 = event.getY();
break;
case(MotionEvent.ACTION_UP): {
x2 = event.getX();
y2 = event.getY();
dx = x2-x1;
dy = y2-y1;
// Use dx and dy to determine the direction of the move
if(Math.abs(dx) > Math.abs(dy)) {
if(dx>0)
direction = "right";
else
direction = "left";
} else {
if(dy>0)
direction = "down";
else
direction = "up";
}
}
}
How can I detect the up-down (Ping-pong) movement of device?
package org.mabna.order.utils;
public interface OnDeviceShakeListener {
public abstract void onDeviceShaked();
}
import java.util.ArrayList;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
public class SensorListener implements SensorEventListener {
float[] xVals = new float[] { 0, 0, 0 };
float[] yVals = new float[] { 0, 0, 0 };
long prevTime = 0;
final float angle = 10;
final long time = 100 * 1000 * 1000; // in nanosecond
public SensorListener()
{
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
if (event.timestamp - prevTime > time)
{
prevTime = event.timestamp;
xVals[0] = xVals[1];
xVals[1] = xVals[2];
xVals[2] = event.values[1];
yVals[0] = yVals[1];
yVals[1] = yVals[2];
yVals[2] = event.values[2];
if (degreeChanged(xVals) ||
degreeChanged(yVals))
{
OnDeviceShaked();
}
}
}
}
}
private boolean degreeChanged(float[] V) {
float v0 = Math.abs(V[0]);
float v1 = Math.abs(V[1]);
float v2 = Math.abs(V[2]);
boolean hasAngle = ((v1 - v0) > angle && (v1 - v2) > angle);
return hasAngle;
}
// DeviceShakeListener ----------------------------------------------------
ArrayList<OnDeviceShakeListener> arrOnDeviceShakeListener =
new ArrayList<OnDeviceShakeListener>();
/**
* Listens if device is shaken like a Ping-pong racket.
*
* @param listener
*/
public void setOnDeviceShakeListener(
OnDeviceShakeListener listener) {
arrOnDeviceShakeListener.add(listener);
}
public void clearOnDeviceShakeListener(
OnDeviceShakeListener listener) {
arrOnDeviceShakeListener.remove(listener);
}
// This function is called after the new point received
private void OnDeviceShaked() {
// Check if the Listener was set, otherwise we'll get an Exception when
// we try to call it
if (arrOnDeviceShakeListener != null) {
// Only trigger the event, when we have any listener
for (int i = arrOnDeviceShakeListener.size() - 1; i >= 0; i--) {
arrOnDeviceShakeListener.get(i).onDeviceShaked();
}
}
}
}
In Activity:
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorHandler = new SensorListener();
sensorHandler.setOnDeviceShakeListener(new OnDeviceShakeListener() {
@Override
public void onDeviceShaked() {
//....
}
});
How to use Accelerometer to measure distance for Android Application Development
You get position by integrating the linear acceleration twice but the error is horrible. It is useless in practice.
Here is an explanation why (Google Tech Talk) at 23:20. I highly recommend this video.
Similar questions:
track small movements of iphone with no GPS
What is the real world accuracy of phone accelerometers when used for positioning?
how to calculate phone's movement in the vertical direction from rest?
iOS: Movement Precision in 3D Space
Android accelerometer detect height?
Assuming you mean you want to detect the height the phone was raised from its staring point, yes. The android accelerometer measures force, more info on how to use it can be found here. Keep in mind that the accelerometer isn't a perfect device, and so your results will be approximations of how much the phone was really moved.
Related Topics
Cli on Dalvikvm Fails on Jni Lib
Outofmemory Exception When Loading Bitmap from External Storage
How to Call One Android Application from Another Android Application
Displaying Contact Number and Contact Name in a Custom List View
Gradle 0.9.+/0.9.2 Error: Unable to Load Class 'Com.Android.Builder.Testing.Api.Deviceprovider'
Mvvmcross Changing Viewmodel Within a Mvxbindablelistview
How to Display Map (Google) on a Phonegap Android Application
Custom Translucent Android Actionbar
Why Isn't My Vector Drawable Scaling as Expected
How to Read Properties Defined in Local.Properties in Build.Gradle
Android Access to Remote SQL Database
Center Align Title in Action Bar Using Styles in Android
See Android Recent Task Executed by the User
Batch Scripting - If Exist ./Sdcard/File.Any Using Adb
Android Mediaplayer Problems :"Error (-38 , 0) " and "Stop Called in State 1"