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:
1 2 3 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">SensorManager senserManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);</span> |
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:
1 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">Sensor sensor = senserManager.getDefaultSensor(Sensor.TYPE_LIGHT);</span> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">SensorEventListener listener = new SensorEventListener() { @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { } };</span> |
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:
1 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">senserManager.registerListener(listener, senser, SensorManager.SENSOR_DELAY_NORMAL);</span> |
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:
1 |
<span style="font-family: 'times new roman', times, serif; font-size: 16px;">sensorManager.unregisterListener(listener);</span> |
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
Select the empty activity and press the next button
Set the application name in my case I set as “LightSensor”, and press the finish button.
Our project is successfully created
Now modify the code in activity_main.xml as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/light_level" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="20sp" /> </RelativeLayout> </span> |
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 MainActivity.java as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">package com.example.lightsensor; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private SensorManager sensorManager; private TextView lightLevel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lightLevel = (TextView) findViewById(R.id.light_level); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onDestroy() { super.onDestroy(); if (sensorManager != null) { sensorManager.unregisterListener(listener); } } private SensorEventListener listener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { // The value of the first subscript in the values array is the current light intensity float value = event.values[0]; lightLevel.setText("Current light level is " + value + " lx"); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; } </span> |
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 intensity outside the room
Related Article:
Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth