public class MainActivity extends Activity implements SensorEventListener {
private TextView temperaturelabel;
private SensorManager sensormanager;
private Sensor temperature;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temperaturelabel = (TextView) findViewById(R.id.text);
sensormanager = (SensorManager)getSystemService(SENSOR_SERVICE);
temperature= sensormanager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
temperaturelabel.setText(""+temperature.getPower());
}
protected void onResume() {
super.onResume();
sensormanager.registerListener(this, temperature, SensorManager.SENSOR_DELAY_FASTEST);
}
protected void onPause() {
super.onPause();
sensormanager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_AMBIENT_TEMPERATURE) return;
temperaturelabel.setText(""+temperature.getPower());
}
}
我想從設備獲取溫度。Android上的溫度傳感器
我寫了這段代碼,我試了一下HTC One X,但沒有奏效。
您可以發佈輸出或錯誤。 – prayagupd 2012-08-14 09:38:52
沒有做。沒有錯誤。沒有通知。沒有結果 – Furkan 2012-08-14 09:39:50
HTC One X是否有溫度傳感器?當然是 – nkr 2012-08-14 09:49:05