在 android 工作室中使用加速度计访问设备移动了多少?
Access how much a device moved using accelerometer in android studio?
(我是android的初学者,请放轻松)
我正在制作一个需要精确定位和计算两个非常接近的位置之间的差异的应用程序。我非常怀疑 GPS 能否实现这种精度,目前我正在使用具有良好加速度计的高端手机。基本上,我如何极其精确地访问设备移动了多少?
您可以从这里开始,只获取加速度计。我以前没有用过这个功能。
public class yourActivity extends Activity implements SensorEventListener{
private SensorManager sensorManager;
double ax,ay,az; // these are the acceleration in x,y and z axis
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
ax=event.values[0];
ay=event.values[1];
az=event.values[2];
}
}
}
这里有一份 link 的指南,看起来非常详尽,可能会帮助您顺利完成任务。 Using The Accelerometer
它会起作用
private SensorManager sensorMan;
private Sensor accelerometer;
private float[] mGravity;
private float mAccel;
private float mAccelCurrent;
private float mAccelLast;
// In onCreate method
sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccel = 0.00f;
mAccelCurrent = SensorManager.GRAVITY_EARTH;
mAccelLast = SensorManager.GRAVITY_EARTH;
// And these:
@Override
public void onResume() {
super.onResume();
sensorMan.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onPause() {
super.onPause();
sensorMan.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
mGravity = event.values.clone();
// Shake detection
float x = mGravity[0];
float y = mGravity[1];
float z = mGravity[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = FloatMath.sqrt(x*x + y*y + z*z);
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;
// Make this higher or lower according to how much
// motion you want to detect
if(mAccel > 3){
// do something
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// required method
}
(我是android的初学者,请放轻松)
我正在制作一个需要精确定位和计算两个非常接近的位置之间的差异的应用程序。我非常怀疑 GPS 能否实现这种精度,目前我正在使用具有良好加速度计的高端手机。基本上,我如何极其精确地访问设备移动了多少?
您可以从这里开始,只获取加速度计。我以前没有用过这个功能。
public class yourActivity extends Activity implements SensorEventListener{
private SensorManager sensorManager;
double ax,ay,az; // these are the acceleration in x,y and z axis
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
ax=event.values[0];
ay=event.values[1];
az=event.values[2];
}
}
}
这里有一份 link 的指南,看起来非常详尽,可能会帮助您顺利完成任务。 Using The Accelerometer
它会起作用
private SensorManager sensorMan;
private Sensor accelerometer;
private float[] mGravity;
private float mAccel;
private float mAccelCurrent;
private float mAccelLast;
// In onCreate method
sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccel = 0.00f;
mAccelCurrent = SensorManager.GRAVITY_EARTH;
mAccelLast = SensorManager.GRAVITY_EARTH;
// And these:
@Override
public void onResume() {
super.onResume();
sensorMan.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onPause() {
super.onPause();
sensorMan.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
mGravity = event.values.clone();
// Shake detection
float x = mGravity[0];
float y = mGravity[1];
float z = mGravity[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = FloatMath.sqrt(x*x + y*y + z*z);
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;
// Make this higher or lower according to how much
// motion you want to detect
if(mAccel > 3){
// do something
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// required method
}