如何在 Android 1.5 中正确访问加速度计?

发布于 2024-08-20 12:27:16 字数 2795 浏览 2 评论 0原文

我看过一些 1.0 / 1.1 的代码示例,但由于其中很多现在已被弃用,我希望找到一个不错的 1.5+ 示例。我在这里写了一些代码,但它并没有真正正常工作。任何帮助都会很棒,谢谢!

public class collectAccel extends Activity implements SensorEventListener, 
                                                OnClickListener{

private SensorManager sensorMgr;
private TextView xLabel, yLabel, zLabel;
private Button StartBtn;
private List<Sensor> sensorList;

private float x, y, z;
private long lastUpdate = -1;
// deltas for calibration
private float cx, cy, cz;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    xLabel = (TextView) findViewById(R.id.x_label);
    yLabel = (TextView) findViewById(R.id.y_label);
    zLabel = (TextView) findViewById(R.id.z_label);
    StartBtn = (Button) findViewById(R.id.Button1);
    StartBtn.setOnClickListener(this);
}


@Override
public void onClick(View arg0) {
    if (xLabel.getVisibility() != 1)
        xLabel.setVisibility(1);
    if (yLabel.getVisibility() != 1)
        yLabel.setVisibility(1);
    if (zLabel.getVisibility() != 1)
        zLabel.setVisibility(1);
}

@Override
protected void onPause() {
    super.onPause();

    sensorMgr.unregisterListener((SensorEventListener)this, sensorList.get(0));
    sensorMgr = null;

    cx = 0;
    cy = 0;
    cz = 0;
}

@Override
protected void onResume() {
    super.onResume();
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorList = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);

    boolean accelSupported = sensorMgr.registerListener((SensorEventListener)this, 
            sensorList.get(0),
            SENSOR_DELAY_UI);

    if (!accelSupported) {
        // on accelerometer on this device
        sensorMgr.unregisterListener((SensorEventListener)this, sensorList.get(0));
    }
}



@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    cx = 0;
    cy = 0;
    cz = 0;

}


@Override
public void onSensorChanged(SensorEvent arg0) {
    if (arg0.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        long curTime = System.currentTimeMillis();
        // only allow one update every 100ms, otherwise updates
        // come way too fast and the phone gets bogged down
        // with garbage collection
        if (lastUpdate == -1 || (curTime - lastUpdate) > 100) {
            lastUpdate = curTime;

            x = arg0.values[0];
            y = arg0.values[1];
            z = arg0.values[2];
            xLabel.setText(String.format("X: %+2.5f (%+2.5f)", (x+cx), cx));
            yLabel.setText(String.format("Y: %+2.5f (%+2.5f)", (y+cy), cy));
            zLabel.setText(String.format("Z: %+2.5f (%+2.5f)", (z+cz), cz));
        }
    }
}
}

I have seen a few code examples of 1.0 / 1.1 but since a lot of that is deprecated now I was hoping to find a nice 1.5+ example. I have some code here that I wrote but it doesn't really work properly. Any help would be great, thanks!

public class collectAccel extends Activity implements SensorEventListener, 
                                                OnClickListener{

private SensorManager sensorMgr;
private TextView xLabel, yLabel, zLabel;
private Button StartBtn;
private List<Sensor> sensorList;

private float x, y, z;
private long lastUpdate = -1;
// deltas for calibration
private float cx, cy, cz;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    xLabel = (TextView) findViewById(R.id.x_label);
    yLabel = (TextView) findViewById(R.id.y_label);
    zLabel = (TextView) findViewById(R.id.z_label);
    StartBtn = (Button) findViewById(R.id.Button1);
    StartBtn.setOnClickListener(this);
}


@Override
public void onClick(View arg0) {
    if (xLabel.getVisibility() != 1)
        xLabel.setVisibility(1);
    if (yLabel.getVisibility() != 1)
        yLabel.setVisibility(1);
    if (zLabel.getVisibility() != 1)
        zLabel.setVisibility(1);
}

@Override
protected void onPause() {
    super.onPause();

    sensorMgr.unregisterListener((SensorEventListener)this, sensorList.get(0));
    sensorMgr = null;

    cx = 0;
    cy = 0;
    cz = 0;
}

@Override
protected void onResume() {
    super.onResume();
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorList = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);

    boolean accelSupported = sensorMgr.registerListener((SensorEventListener)this, 
            sensorList.get(0),
            SENSOR_DELAY_UI);

    if (!accelSupported) {
        // on accelerometer on this device
        sensorMgr.unregisterListener((SensorEventListener)this, sensorList.get(0));
    }
}



@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    cx = 0;
    cy = 0;
    cz = 0;

}


@Override
public void onSensorChanged(SensorEvent arg0) {
    if (arg0.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        long curTime = System.currentTimeMillis();
        // only allow one update every 100ms, otherwise updates
        // come way too fast and the phone gets bogged down
        // with garbage collection
        if (lastUpdate == -1 || (curTime - lastUpdate) > 100) {
            lastUpdate = curTime;

            x = arg0.values[0];
            y = arg0.values[1];
            z = arg0.values[2];
            xLabel.setText(String.format("X: %+2.5f (%+2.5f)", (x+cx), cx));
            yLabel.setText(String.format("Y: %+2.5f (%+2.5f)", (y+cy), cy));
            zLabel.setText(String.format("Z: %+2.5f (%+2.5f)", (z+cz), cz));
        }
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2024-08-27 12:27:16

您可以在 Android 参考 中查找它,但它看起来像您 如果您确实想要该列表,则需要

SensorManager mgr = Context.getSystemService(SENSOR_SERVICE);
IList<Sensor> sensorList = mgr.getSensorList(SensorManager.SENSOR_ACCELEROMETER);

,但我认为您实际上应该使用SensorManager.RegisterListener(SensorListenerlistener, intsensors,intrate)或该方法的另一个版本。

You can look it up in the Android Reference but It looks like you'll need

SensorManager mgr = Context.getSystemService(SENSOR_SERVICE);
IList<Sensor> sensorList = mgr.getSensorList(SensorManager.SENSOR_ACCELEROMETER);

If you really want the list, but I think you are actually meant to use SensorManager.RegisterListener(SensorListener listener, int sensors, int rate) or another version of that method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文