I\'m just wondering if it is possible / where would I get started in measuring the upwards and downwards movement of an Android device. I\'d need it to be as accurate as possibl
Before,We have to know about sensor API which is we are using.
Android Sensor API
Android sensor API provides many classes and interface. The important classes and interfaces of sensor API are as follows:
1) SensorManager class
The android.hardware.SensorManager
class provides methods :
to get sensor instance,
to access and list sensors,
to register and unregister sensor listeners etc.
You can get the instance of SensorManager by calling the method getSystemService()
and passing the SENSOR_SERVICE
constant in it.
SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
2) Sensor class
The android.hardware.Sensor
class provides methods to get information of the sensor such as sensor name, sensor type, sensor resolution, sensor type etc.
3) SensorEvent class
Its instance is created by the system. It provides information about the sensor.
4) SensorEventListener interface
It provides two call back methods to get information when sensor values (x,y and z) change or sensor accuracy changes. Public and abstract methods Description
void onAccuracyChanged(Sensor sensor, int accuracy)
//it is called when sensor accuracy is changed.
*void onSensorChanged(SensorEvent event)*
//it is called when sensor values are changed.
Note: assume *X=horizontal side,Y=Vertical side and Z=elevation * of the device.
create a simple xml like active_main.xml
Next create a simple activity like MainActivity.java to measure directions like downward/upwards.
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;
import android.hardware.Sensor;
import java.util.List;
public class MainActivity extends Activity {
SensorManager sm = null;
TextView textView1 = null;
List list;
SensorEventListener sel = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
textView1.setText("x: "+values[0]+"\ny: "+values[1]+"\nz: "+values[2]);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Get a SensorManager instance */
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
textView1 = (TextView)findViewById(R.id.textView1);
list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(list.size()>0){
sm.registerListener(sel, (Sensor) list.get(0), SensorManager.SENSOR_DELAY_NORMAL);
}else{
Toast.makeText(getBaseContext(), "Error: No Accelerometer.", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onStop() {
if(list.size()>0){
sm.unregisterListener(sel);
}
super.onStop();
}
}
Output seems like:
X=0.0[No Move On Horizontal]
Y=9.77622[Moved on Vertically]
Z=0.813417[Move on Elevation]