Android 访问加速计:始终返回 0 作为值
我想在 Android 手机中为我的应用程序使用加速度计。 来自传感器的数据将与 GPS 点一起保存,因此仅在更新 GPS 点时才需要该值。
如果我使用附加的代码,则值始终为零。 API 级别 8 权限:互联网、良好位置 测试设备:Galaxy S(i9000)、Nexus One
有什么建议吗?我被困在这一点上。
来自德国的最诚挚的问候, 帕斯卡
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
public class AccelerometerService extends Activity{
AccelerometerData accelerometerData;
private SensorManager mSensorManager;
private float x,y,z;
private class AccelerometerData implements SensorEventListener{
public void onSensorChanged(SensorEvent event) {
x = event.values[0];
y = event.values[1];
z = event.values[2];
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(accelerometerData,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(accelerometerData);
super.onStop();
}
public String getSensorString()
{
return ("X: " + x+"m/s, Y: "+ y +"m/s, Z: "+ z +"m/s" );
}
}
i would like to use the accelerometer in an Android Handset for my Application.
The data from the sensor will be saved together with a GPS Point, so the Value is only needed when the GPS Point is updated.
If i use the attached Code the values is always zero.
API Level 8
Permissions: Internet, Fine Location
Testing Device: Galaxy S(i9000), Nexus One
Any Suggestions? I am stuck at this point.
Best regards from Germany,
Pascal
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
public class AccelerometerService extends Activity{
AccelerometerData accelerometerData;
private SensorManager mSensorManager;
private float x,y,z;
private class AccelerometerData implements SensorEventListener{
public void onSensorChanged(SensorEvent event) {
x = event.values[0];
y = event.values[1];
z = event.values[2];
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(accelerometerData,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(accelerometerData);
super.onStop();
}
public String getSensorString()
{
return ("X: " + x+"m/s, Y: "+ y +"m/s, Z: "+ z +"m/s" );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在任何时候为
accelerometerData
赋值?我本来希望它在 onCreate 方法中分配。您可能还需要考虑屏幕方向并过滤掉传入的任何非加速度计事件(我不确定过滤是否绝对必要,但它位于 加速计 Play SDK 示例。
Do you assign a value to
accelerometerData
at any point? I would have expected it to be assigned in theonCreate
method.You may also need to account for the screen orientation and filter out any non-accelerometer events that come in (I'm not sure if the filtering is absolutely necessary, but it's in the Accelerometer Play SDK sample.