我完全输了!对我来说,这段代码是完美的...但是唉 - 没有System.out的!??!
是的,我有一个加速度计:)
public class MainActivity extends Activity implements SensorEventListener
{
private final static String CLASS_NAME = "(MainActivity)";
private SensorManager m_oSensorManager;
private Sensor m_oAccelerometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_oSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
m_oAccelerometer = m_oSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
m_oSensorManager.registerListener(this, m_oAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent arg0) {
System.out.println(CLASS_NAME + " onSensorChanged()");
}
}
忽略这些行.....避免堆栈溢出过滤器.....
答案 0 :(得分:2)
尝试
Log.i(CLASS_NAME, "onSensorChanged()");
答案 1 :(得分:1)
处理程序寄存器调用必须在onResume()中。我确定我已经尝试过了,所以这可能是我的手机弄乱了我的大脑。但无论如何,对于其他人......
@Override
public void onResume()
{
super.onResume();
m_oSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
m_oAccelerometer = m_oSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
m_oMagnetometer = m_oSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
m_oSensorManager.registerListener(this, m_oAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
m_oSensorManager.registerListener(this, m_oMagnetometer,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onPause()
{
super.onPause();
m_oSensorManager.unregisterListener(this);
}