Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android light sensor - detect significant light changes

I am new to sensor use in android and now a bit confused. I need to perform some actions only if there's a significant light change, e.g. the light was turned on in a dark room. I have a pretty simple default implementation so far. How can I tell the system I only want to react to a significant light change?

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
    }

  }

}
like image 336
Droidman Avatar asked Jul 01 '13 18:07

Droidman


1 Answers

try the below code :-

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]);
    }
   }

    };

}
like image 108
Yogesh Tatwal Avatar answered Oct 13 '22 13:10

Yogesh Tatwal