Created
May 27, 2017 21:36
-
-
Save ssaurel/2a29586c131cbb954f413425ff5a45a5 to your computer and use it in GitHub Desktop.
Main Activity for a Luminosity Detector App Tutorial on the SSaurel's Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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