1

我已经看到了一些 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));
        }
    }
}
}
4

1 回答 1

1

您可以在Android 参考中查找它,但看起来您需要

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

如果您真的想要该列表,但我认为您实际上是打算使用SensorManager.RegisterListener(SensorListener listener, int sensors, int rate)该方法或该方法的其他版本。

于 2010-02-05T05:52:05.217 回答