Android Light Sensor - Detect significant lighting changes

I am new to using sensors in Android and a little confused. I need to perform some actions only with a significant change in light, for example. the light turned on in a dark room. So far, I have a pretty simple default implementation. How can I tell the system that I only want to respond to a significant change in light?

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 } } } 
+4
source share
2 answers

try the code below: -

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

If there was just a change in light, you can simply check if the light sensor value is set to 0. If you find some strange change, for example, 27-20 for a certain reason, then I think you may have to use the machine training.

0
source

Source: https://habr.com/ru/post/1489147/


All Articles