Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Main Activity for a Luminosity Detector App Tutorial on the SSaurel's Channel
package com.ssaurel.sensors;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private SensorManager sensorManager;
private Sensor lightSensor;
private SensorEventListener lightEventListener;
private View root;
private float maxValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = findViewById(R.id.root);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if (lightSensor == null) {
Toast.makeText(this, "The device has no light sensor !", Toast.LENGTH_SHORT).show();
finish();
}
// max value for light sensor
maxValue = lightSensor.getMaximumRange();
lightEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float value = sensorEvent.values[0];
getSupportActionBar().setTitle("Luminosity : " + value + " lx");
// between 0 and 255
int newValue = (int) (255f * value / maxValue);
root.setBackgroundColor(Color.rgb(newValue, newValue, newValue));
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(lightEventListener, lightSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(lightEventListener);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment