使用接近传感器和onSensorChanged

时间:2015-08-30 11:28:54

标签: android proximity proximitysensor

我正在使用接近传感器来增加TextView中的数字,它可以工作但是它以2的增量触发,即0 2 4 6 8。我想获得一个增量,是否可能?

TextView text ;
SensorManager sm ;
Sensor Proxsens;
int i=0 ;

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

    text =(TextView)findViewById(R.id.text_id);
    sm = (SensorManager)getSystemService(SENSOR_SERVICE);
    Proxsens = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);

    sm.registerListener(this,Proxsens,SensorManager.SENSOR_DELAY_NORMAL);

}

@Override
public void onSensorChanged(SensorEvent event) {

    text.setText(String.valueOf(i));
    i++;
//  text.setText(String.valueOf(event.values[0]));
}

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

}

1 个答案:

答案 0 :(得分:-1)

@Override
public void onSensorChanged(SensorEvent event){
    if (event.values[0] == 0) {
        text.setText(String.valueOf(i));
        i++;
    }
}