Environmental Sensor
The light sensor measures ambient light in
lux
, a standard unit of illumination. The light sensor typically is used to automatically adjust screen brightness.The proximity sensor measures when the device is close to another object. The proximity sensor is often used to turn off touch events on a phone's screen when you answer a phone call, so that touching your phone to your face does not accidentally launch apps or otherwise interfere with the device's operation.
Firstly, let's use replace the entire ScrollView
component. with the block of code below in activity_main.xml
but what if our phones don't have this sensor?!?!?!
We'll need to declare the Sensors
themselves
Let's also get the view of our TextView
s
Let's enable the Sensors
and TextViews
by assigning them in onCreate()
in MainActivity.class
Here we are testing for the existences of the sensors
SensorEventListener
interface allows us to listen for events that the Android sensor framework generates.
onSensorChanged()
: Called when new sensor data is available. You will use this callback most often to handle new sensor data in your app.onAccuracyChanged()
: Called if the sensor's accuracy changes, so your app can react to that change. Most sensors, including the light and proximity sensors, do not report accuracy changes. In this app, you leaveonAccuracyChanged()
empty.
Note that we are overriding the onStart()
activity lifecycle method to register your sensor listeners.
This allows us to ensure sensors to continue running even if the app is in multiwindow mode which uses onResume()
and onPause()
Listening to incoming sensor data uses device power and consumes battery life.
Don't register your listeners in onCreate()
, as that would cause the sensors to be on and sending data (using device power) even when your app was not in the foreground. Use the onStart()
and onStop()
methods to register and unregister your sensor listeners.
Each sensor that your app uses needs its own listener, and you should make sure that those sensors exist before you register a listener for them. Use the registerListener()
method from the SensorManager
to register a listener. This method takes three arguments:
An app or activity
Context
. You can use the current activity (this
) as the context.The
Sensor
object to listen to.A delay constant from the
SensorManager
class. The delay constant indicates how quickly new data is reported from the sensor. Sensors can report a lot of data very quickly, but more reported data means that the device consumes more power. Make sure that your listener is registered with the minimum amount of new data it needs. In this example you use the slowest value (SensorManager.SENSOR_DELAY_NORMAL
). For more data-intensive apps such as games, you may need a faster rate such asSENSOR_DELAY_GAME
orSENSOR_DELAY_FASTEST
.
Implement the onStop()
lifecycle method to unregister your sensor listeners when the app pauses: which prevents the device from using power when the app is not visible.
onSensorChange():
SensorEvent
object includes important properties of the event, such as which sensor is reporting new data, and the new data values. Use the sensor
property of the SensorEvent
to get a Sensor
object, and then use getType()
We use values[0]
because the light sensor only has one perimeter values to return unlike an accelerator which has 3 values for its axis, where we will use values[0]
, values[1]
, and values[2]
instead
When you call getString()
to get the string from the resources, you include values to substitute into the string where the placeholder codes are. The part of the string that is not made up of placeholders ("Light Sensor: "
) is passed through to the new string.
Last updated