2. Sensor learning notes light sensors, sensor learning notes light
In this lesson, we are going to learn about the lighting sensors in Android. If you already have an answer, skip this article.
Android supports automatic screen brightness adjustment. It detects the illumination intensity of the environment around the mobile phone, and adjusts the brightness of the mobile phone screen accordingly to ensure that the mobile phone screen can be clearly viewed in light or low light.
Too much theory is a bit false. Here we will make a project of practical lighting sensors.
Objective: To compile a simple light detector program so that the mobile phone can detect changes in the illumination intensity of the surrounding environment.
:
Note:
When the light is shining above, the value changes.
Tip:
The simulator cannot perceive the illumination intensity, so it is recommended to run on a real mobile phone.
Undergraduate source code download: https://github.com/fairyxingyun/Android_Sensors_API
1. First, create an android project:
2. Use TextView to display the current illumination intensity
Here, a TextView control is used to display the illumination intensity.
The layout file of activity_main is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xingyun.androidlightsensordemo.MainActivity" > <TextView android:id="@+id/light_level_id" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerInParent="true" android:textSize="20sp"/></RelativeLayout>
3. MainActivity. java background processing code:
Package com. xingyun. androidlightsensordemo; 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. support. v7.app. actionBarActivity; import android. widget. textView; public class MainActivity extends ActionBarActivity {private TextView sho WLightLevelTextView; // display illumination Sensor strength private SensorManager sensorManager; // Sensor manager private Sensor lightSensor; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); showLightLevelTextView = (TextView) findViewById (R. id. light_level_id ); /*************************************** * ************************************ Se NsorManager is the manager of all the sensors in the system. With its instance, ** you can call the getdefasensensor () method to obtain any Sensor type. TYPE_LIGHT constant to specify the sensor type ********************************** ***************************************/ // obtain the SensorManager instance sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); // specify the sensor type as lightSensor = sensorManager. getdefasensensor (Sensor. TYPE_LIGHT);}/** this method is called when the activity is ready to interact with the user. At this time, the activity must be at the top of the returned stack and in the running state. **/@ Override protected void onResume () {// TODO Auto-generated method stub super. onResume ();/** the registerListener () method receives three parameters. The first parameter is the instance of SensorEventListener. * The second parameter is the instance of Sensor, the two parameters are successfully obtained. * The third parameter is used to indicate the update rate of sensor output information, and their update rate increases sequentially. * /// Call the registerListener () method of SensorManager to register the listener for sensorManager to take effect. registerListener (listener, lightSensor, SensorManager. SENSOR_DELAY_NORMAL);}/** this method is called when the system is ready to start or resume another activity. * In this method, we usually release CPU-consuming resources and save some key data. However, the execution speed of this method must be fast, otherwise, it will affect the use of the new stack top activity */@ Override protected void onPause () {// TODO Auto-generated method stub super. onPause (); sensorManager. unregisterListener (listener);} // this method is called before the activity is destroyed, and the activity status changes to the destruction status. @ Override protected void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); if (sensorManager! = Null) {sensorManager. unregisterListener (listener) ;}}/*** needs to listen on the signal output by the sensor, which must be achieved through SensorEventListener. SensorEventListener * is an interface that defines the onSensorChanged () and onAccuracyChanged () Methods */private SensorEventListener listener = new SensorEventListener () {// when the sensor's precision changes, the onAccuracyChanged () method will be called, @ Override public void onSensorChanged (SensorEvent event) {// TODO Auto-generated method stub /****************************** * **************************** onSensorChanged () method. Contains a values array. All the information output by the sensor is stored here. **************************************** * ***************/float value = event. values [0]; // display the current screen brightness value showLightLevelTextView. setText ("current illumination intensity:" + value + "lx");} // The onSensorChanged () method is called when the value monitored by the sensor changes. @ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {// TODO Auto-generated method stub }};}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.