0

我正在尝试构建一个应用程序,我需要使用加速度计。使用传感器模拟器,加速度计工作正常,但是当我制作 apk 时,加速度计的输出不起作用。我使用的是三星 Exibit 4g XII,这个设备有加速度计

public class AcelerometerImp extends Activity implements SensorEventListener {

    StringBuilder builder;
    TextView textView;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        textView = new TextView(this);
        setContentView(textView);
        SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        textView.setText( (manager.getSensorList( Sensor.TYPE_ACCELEROMETER ).size() == 0) 
                ? "No hay acelerometro" : manager.getSensorList( Sensor.TYPE_ACCELEROMETER ).get(0).toString()  );
    }

    @Override
    public void onSensorChanged (SensorEvent event){
        builder.setLength(0);
        builder.append("x: " + event.values[0] + ", y: " + event.values[1] + ", z: " + event.values[2]);
        textView.setText(builder.toString());
    }

    @Override
    public void onAccuracyChanged (Sensor sensor, int accuracy){        
    }
}
4

1 回答 1

1

您必须注册监听器。

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

    manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
                                    SensorManager.SENSOR_DELAY_NORMAL);
}  

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

    manager.unregisterListener(this);
}
于 2013-03-31T16:36:54.830 回答