public class SensorActivity extends Activity implements SensorEventListener { private final SensorManager sensorManager; private final Sensor lightSensor; public SensorActivity() { sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); lightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); // } protected void onResume() { super.onResume(); sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); // } protected void onPause() { super.onPause(); sensorManager.unregisterListener(this); // } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { // SensorEvent } }
public class LightSensorManager implements SensorEventListener { private enum Environment {DAY, NIGHT} public interface EnvironmentChangedListener { void onDayDetected(); void onNightDetected(); } private static final int THRESHOLD_LUX = 50; private static final String TAG = "LightSensorManager"; private final SensorManager sensorManager; private final Sensor lightSensor; private EnvironmentChangedListener environmentChangedListener; private Environment currentEnvironment; public LightSensorManager(Context context) { sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); // } public void enable() { if (lightSensor != null){ sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); } else { Log.w(TAG, "Light sensor in not supported"); } } public void disable() { sensorManager.unregisterListener(this); } public EnvironmentChangedListener getEnvironmentChangedListener() { return environmentChangedListener; } public void setEnvironmentChangedListener(EnvironmentChangedListener environmentChangedListener) { this.environmentChangedListener = environmentChangedListener; } @Override public void onSensorChanged(SensorEvent event) { float luxLevel = event.values[0]; Environment oldEnvironment = currentEnvironment; currentEnvironment = luxLevel < THRESHOLD_LUX ? Environment.NIGHT : Environment.DAY; if (hasChanged(oldEnvironment, currentEnvironment)){ callListener(currentEnvironment); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} private boolean hasChanged(Environment oldEnvironment, Environment newEnvironment) { return oldEnvironment != newEnvironment; } private void callListener(Environment environment) { if (environmentChangedListener == null || environment == null){ return; } switch (environment) { case DAY: environmentChangedListener.onDayDetected(); break; case NIGHT: environmentChangedListener.onNightDetected(); break; } } }
public class LightSensorManager implements SensorEventListener { private enum Environment {DAY, NIGHT} public interface EnvironmentChangedListener { void onDayDetected(); void onNightDetected(); } private static final int THRESHOLD_DAY_LUX = 50; private static final int THRESHOLD_NIGHT_LUX = 40; private static final String TAG = "LightSensorManager"; private final SensorManager sensorManager; private final Sensor lightSensor; private EnvironmentChangedListener environmentChangedListener; private Environment currentEnvironment; public LightSensorManager(Context context) { sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); // } public void enable() { if (lightSensor != null){ sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); } else { Log.w(TAG, "Light sensor in not supported"); } } public void disable() { sensorManager.unregisterListener(this); } public EnvironmentChangedListener getEnvironmentChangedListener() { return environmentChangedListener; } public void setEnvironmentChangedListener(EnvironmentChangedListener environmentChangedListener) { this.environmentChangedListener = environmentChangedListener; } @Override public void onSensorChanged(SensorEvent event) { float luxLevel = event.values[0]; Environment oldEnvironment = currentEnvironment; if (luxLevel < THRESHOLD_NIGHT_LUX){ currentEnvironment = Environment.NIGHT; } else if (luxLevel > THRESHOLD_DAY_LUX){ currentEnvironment = Environment.DAY; } if (hasChanged(oldEnvironment, currentEnvironment)){ callListener(currentEnvironment); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} private boolean hasChanged(Environment oldEnvironment, Environment newEnvironment) { return oldEnvironment != newEnvironment; } private void callListener(Environment environment) { if (environmentChangedListener == null || environment == null){ return; } switch (environment) { case DAY: environmentChangedListener.onDayDetected(); break; case NIGHT: environmentChangedListener.onNightDetected(); break; } } }
public class LightSensorManager implements SensorEventListener { private enum Environment {DAY, NIGHT} public interface EnvironmentChangedListener { void onDayDetected(); void onNightDetected(); } private static final float SMOOTHING = 10; private static final int THRESHOLD_DAY_LUX = 50; private static final int THRESHOLD_NIGHT_LUX = 40; private static final String TAG = "LightSensorManager"; private final SensorManager sensorManager; private final Sensor lightSensor; private EnvironmentChangedListener environmentChangedListener; private Environment currentEnvironment; private final LowPassFilter lowPassFilter; public LightSensorManager(Context context) { sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); lowPassFilter = new LowPassFilter(SMOOTHING); } public void enable() { if (lightSensor != null){ sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); } else { Log.w(TAG, "Light sensor in not supported"); } } public void disable() { sensorManager.unregisterListener(this); } public EnvironmentChangedListener getEnvironmentChangedListener() { return environmentChangedListener; } public void setEnvironmentChangedListener(EnvironmentChangedListener environmentChangedListener) { this.environmentChangedListener = environmentChangedListener; } @Override public void onSensorChanged(SensorEvent event) { float luxLevel = event.values[0]; luxLevel = lowPassFilter.submit(luxLevel); Environment oldEnvironment = currentEnvironment; if (luxLevel < THRESHOLD_NIGHT_LUX){ currentEnvironment = Environment.NIGHT; } else if (luxLevel > THRESHOLD_DAY_LUX){ currentEnvironment = Environment.DAY; } if (hasChanged(oldEnvironment, currentEnvironment)){ callListener(currentEnvironment); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} private boolean hasChanged(Environment oldEnvironment, Environment newEnvironment) { return oldEnvironment != newEnvironment; } private void callListener(Environment environment) { if (environmentChangedListener == null || environment == null){ return; } switch (environment) { case DAY: environmentChangedListener.onDayDetected(); break; case NIGHT: environmentChangedListener.onNightDetected(); break; } } } public class LowPassFilter { private float filteredValue; private final float smoothing; private boolean firstTime = true; public LowPassFilter(float smoothing) { this.smoothing = smoothing; } public float submit(float newValue){ if (firstTime){ filteredValue = newValue; firstTime = false; return filteredValue; } filteredValue += (newValue - filteredValue) / smoothing; return filteredValue; } }
Source: https://habr.com/ru/post/255961/
All Articles