我在模拟器上运行此代码以读取温度变化,但它从未检测到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);
}
}
谢谢
最佳答案
创建仿真器时,是否提供了硬件?
打开AVD,找到“硬件”部分,单击“新建”,然后添加“温度支持”。
关于android - 通过模拟器设置温度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10377690/