0
這可能是個愚蠢的問題,但我無法自己處理。我設法獲得了Light Sensor的價值。我需要得到這個值作爲變量,但我不能訪問變量X.你能幫我嗎?如何獲取傳感器值作爲變量
公共類lightWakeup擴展AppCompatActivity {
ImageButton imageButton;
Intent navrat;
MediaPlayer mp;
TextView text;
SensorManager sensorManager;
Sensor sensor;
float x;
public float c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light_wakeup);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
text = (TextView)findViewById(R.id.textView);
//zapnuti hudby
mp = MediaPlayer.create(this, R.raw.hypnothis);
mp.setVolume(50, 50);
mp.start();
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
text.setText(String.valueOf(c));
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.stop();
navrat = new Intent(getApplicationContext(), MainPage.class);
startActivity(navrat);
}
});
}
public void onResume() {
super.onResume();
sensorManager.registerListener(lightListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
public void onStop() {
super.onStop();
sensorManager.unregisterListener(lightListener);
}
public SensorEventListener lightListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int acc) { }
public void onSensorChanged(SensorEvent event) {
x = event.values[0];
text.setText((int)x);
}
};
}
看到關於變量修飾符謝謝,我明顯有很多東西要學。 –
不客氣,繼續前進! – lfaletti