Android光线传感器 - 检测明显的光线变化

时间:2013-07-01 18:50:43

标签: android android-sensors

我是Android中传感器使用的新手,现在有点困惑。我只有在发生明显的光变化时才需要执行某些操作,例如灯在黑暗的房间里打开了。到目前为止,我有一个非常简单的默认实现。我怎么能告诉系统我只想对重大的光变化作出反应?

public class MainActivity extends Activity implements SensorEventListener {

private SensorManager mSensorManager;
private Sensor mLight;
private RelativeLayout mLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    mLayout = (RelativeLayout) findViewById(R.id.mLayout);
    mLayout.setKeepScreenOn(true);
}

@Override
protected void onResume() {
    mSensorManager.registerListener(this, mLight,
            SensorManager.SENSOR_DELAY_FASTEST);
    super.onResume();
}

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

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    if (sensor.getType() == Sensor.TYPE_LIGHT) {

        // TODO
    }

}

@Override
public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_LIGHT) {

        // TODO
    }

  }

}

2 个答案:

答案 0 :(得分:12)

尝试以下代码: -

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textLIGHT_available, textLIGHT_reading;

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

        textLIGHT_available 
         = (TextView)findViewById(R.id.LIGHT_available);
        textLIGHT_reading 
         = (TextView)findViewById(R.id.LIGHT_reading);

     SensorManager mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

     Sensor lightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
     if(lightSensor != null){
      textLIGHT_available.setText("Sensor.TYPE_LIGHT Available");
      mySensorManager.registerListener(
        lightSensorListener, 
        lightSensor, 
        SensorManager.SENSOR_DELAY_NORMAL);

     } else {
      textLIGHT_available.setText("Sensor.TYPE_LIGHT NOT Available");
     }
    }

    private final SensorEventListener lightSensorListener
     = new SensorEventListener(){

   @Override
   public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

   }

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

    };

}

答案 1 :(得分:0)

如果只有一个轻微的变化你可以检查一下光传感器值是否为0.如果你因某个特定原因检测到一些奇怪的变化,比如27到20,那么我想你可能需要使用机器学习。