Light Sensor Application Development in Android Studio

Light Sensor Application Development:

Light Sensor Application development in android studio:- The light sensor application in Android is quite common. For example, the system has an automatic adjustment of screen brightness Function. It will detect the light intensity of the environment around the phone, and then adjust the brightness of the phone screen accordingly to ensure It proves that the mobile phone screen can be seen clearly whether it is in strong or weak light. Let’s take a look at what the light sensor is, and How to use it.

How to use the Light Sensor in Application:

The usage of each sensor in Android is actually quite similar. It can really be said to be a Belden. The first step To obtain an instance of SensorManager, the method is as follows:

SensorManager is the manager of all sensors in the system. After having its instance, you can call getDefaultSensor() The method to get any sensor type is as follows:

The Sensor.TYPE_LIGHT constant is used here to specify the sensor type, and the Sensor instance at this time represents a light sensor in application. There are many constants of other sensor types in Sensor, which we will learn slowly in the next articles. Next, we need to monitor the signal output by the sensor, which is achieved with the help of SensorEventListener. SensorEventListener is an interface in which onSensorChanged() and onAccuracyChanged() are defined These two methods are as follows:

When the accuracy of the sensor changes, the onAccuracyChanged() method will be called. When the value changes, the onSensorChanged() method is called. You can see that the onSensorChanged() method is passed in A SensorEvent parameter, this parameter also contains a values ​​array, the information output by all sensors is Stored here.

Next, we also need to call the registerListener() method of SensorManager to register SensorEventListener To make it effective, the registerListener() method receives three parameters, the first parameter is the SensorEventListener Instance, the second parameter is the instance of Sensor, both of which we have successfully obtained earlier. The third parameter is It is used to indicate the update rate of sensor output information. There are SENSOR_DELAY_UI, SENSOR_DELAY_NORMAL, The four values ​​SENSOR_DELAY_GAME and SENSOR_DELAY_FASTEST are optional, and their update rate is Sequentially increasing. Therefore, registering a SensorEventListener can be written as:

In addition, always remember that when the program exits or the sensor is used up, you must call unregisterListener(). The method will release the used resources, as shown below:

Well, basically the usage is like this, let’s try it out below.

Make a simple Light Sensor Application in Android Studio:

Here we are going to write a simple light detector Application programmed in the android studio so that the mobile phone can detect the intensity of the surrounding environment. Note that since the function of the sensor cannot be simulated in the simulator, it is still recommended that you put the code written in this article is Run on the phone. Create a new project

light sensor application

Select the empty activity and press the next button

light sensor application

Set the application name in my case I set as “LightSensor”, and press the finish button.

light sensor application

Our project is successfully created

light sensor application

Now modify the code in activity_main.xml as follows:

The layout file is also very simple, only one TextView is used to display the current light intensity, and let it be in Show centered in RelativeLayout.

Then modify the code in as follows:

I believe this part of the code must be easy for you to understand. First, we get an instance of the TextView control in the onCreate() method, and then register the listener of the light sensor application, so that when the light intensity changes, the SensorEventListener will be called The onSensorChanged() method and the data we need is stored in the values ​​array of SensorEvent. In this example, there will only be one value in the values ​​array, which is the light intensity currently detected by the mobile phone, expressed in lux As the unit, we can directly display this value on the TextView. Finally, don’t forget that in the onDestroy() method Also call the unregisterListener() method to release the resources used.

Now run the program, and you will see the light intensity in the current environment on your phone. Depending on the environment, The displayed value may be tens to hundreds of lux. And if you use strong light to illuminate the phone, it may reach The light intensity to thousands of lux is shown in the below Figures.

light intensity inside a room

light sensor application

light intensity outside the room

light sensor application

Related Article:

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button