This article describes the method of Android programming using light sensors to obtain light intensity. Share it for your reference, as follows:
In Android development, sometimes we need to know the light intensity of the device's environment, and of course this requires our device to have a light sensor. Usually, the automatic brightness of the screen of our mobile phone is achieved by using a light sensor. The sensor is near the front camera, and in addition, there is a distance sensor. Here we mainly explain how to use the light sensor of Android phones.
The following is a light sensor management class I simply packaged, which mainly provides 3 methods:
1.start()
: Start, call before obtaining the light intensity.
2.stop()
: Stop, call after no longer needing to obtain the lighting intensity.
3.getLux()
: Obtain the light intensity in lux.
If you need some extra methods, you can add them yourself based on the returned light intensity. Below is the entire LightSensorManager class
import ; import ; import ; import ; import ; import ; public class LightSensorManager { private static final boolean DEBUG = true; private static final String TAG = "LightSensor"; private static LightSensorManager instance; private SensorManager mSensorManager; private LightSensorListener mLightSensorListener; private boolean mHasStarted = false; private LightSensorManager() { } public static LightSensorManager getInstance() { if (instance == null) { instance = new LightSensorManager(); } return instance; } public void start(Context context) { if (mHasStarted) { return; } mHasStarted = true; mSensorManager = (SensorManager) ().getSystemService(Context.SENSOR_SERVICE); Sensor lightSensor = (Sensor.TYPE_LIGHT); // Get the light sensor if (lightSensor != null) { // When the light sensor exists mLightSensorListener = new LightSensorListener(); (mLightSensorListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); // Register event monitoring } } public void stop() { if (!mHasStarted || mSensorManager == null) { return; } mHasStarted = false; (mLightSensorListener); } /** * Get light intensity */ public float getLux() { if (mLightSensorListener != null) { return ; } return -1.0f; // Return -1 by default, indicating that the device has no light sensor or is calling the start() method } private class LightSensorListener implements SensorEventListener { private float lux; // Light intensity public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { if (() == Sensor.TYPE_LIGHT) { // Get light intensity lux = [0]; if (DEBUG) { (TAG, "lux : " + lux); } } } } }
For this ray intensity value. The darker the value, the lower the value, the lowest should be 0. There are about 300 indoors during the day. It is for reference only. I don’t know if there will be certain errors for different devices.
The call program will not be posted, and the data can be seen directly through log. This class has been used to automatically adjust the exposure level in my custom camera, and there are still effects.
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android data skills for operating json format》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.