我在模拟器上运行此代码以读取温度变化,但它从未检测到温度变化onSensorChanged()
。我使用这个命令通过 telnet 工具改变温度:
传感器设定温度 1:2:3
我做错什么了?
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mysensor;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mysensor = mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
}
public void printText(String text)
{
EditText et=(EditText)findViewById(R.id.editText1);
et.setText(text);
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
String text=sensor.getName()+"accuracy = " + accuracy;
printText(text);
}
@Override
public final void onSensorChanged(SensorEvent event) {
String text = "OnSensorChanged";
printText(text);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mysensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
谢谢