Learning Android™ Application Programming: A Hands-On Guide to Building Android Applications
by James Talbot, Justin McLean, Jorge Hernandez
Detecting Light Levels
How do you know when a device should go into night mode? Users could indicate this with a setting, but they probably shouldn’t be playing around in the Settings screen of their phone while they’re riding their bike after dark. Most phones have a built-in light sensor, and you can use that to determine when the device should automatically go into and out of night mode.
To get the light value, you must first obtain an instance of the SensorManager, then ask for the type of sensor you’re interested in, and then register interest in receiving sensor values. Every time the sensor values change, a method will be called containing the new values.
To receive changes to sensor values, you implement SensorEventListener. To do this ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access