使用简单算法转换传感器值

时间:2018-08-14 21:08:53

标签: sensor

我目前正在研究光传感器。我设法使传感器正常工作。但是我想通过按一下按钮将其乘以0.002来更改所显示的传感器的值。我似乎无法使按钮的代码倍增,因为它一直说不能将运算符(*)应用于double的java.lang.String。我已经尝试过double.parse,但似乎无法正常工作。这是我的代码:

public class MainActivity extends AppCompatActivity implements SensorEventListener {

TextView textView;

SensorManager sensorManager;
Sensor sensor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);

    sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

    Button button = (Button) findViewById(R.id.button3);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View multiply) {
            textView.setText(Integer.parseInt(textView.getText().toString()*0.002));

        }
    });
}

@Override
protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

@Override
protected void onResume() {
    super.onResume();
    sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    if (sensorEvent.sensor.getType() == Sensor.TYPE_LIGHT) {
        textView.setText("" + sensorEvent.values[0]);
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}

0 个答案:

没有答案