Detailed explanation of light sensor usage for Android programming

This paper describes the use of light sensor in Android programming. Share with you for your reference, as follows:

Android phones come with a light sensor. Usually, the automatic brightness of the screen of our phones is realized by a light sensor. The sensor is near the front camera. In addition, there is a distance sensor. Here we mainly explain how to use the light sensor of Android phone.

Get sensor service

To use light sensor in Android development, you need to obtain the system sensor service context.sensor first_ Service is obtained as follows:

Get light sensor

Sensormanager is the system sensor service and the manager of all sensors in the system. Through it, we can obtain a specific type of sensor, and the method to obtain a light sensor is as follows:

Sensor.type in code_ Light refers to the light sensor.

There are many other sensors in the sensor, which will not be discussed here.

Write listener

After we have the sensor object, we need to listen to the sensor. We use the sensoreventlistener interface. There are mainly two methods: onsensorchanged() and onaccuracychanged(). The code is as follows:

Register listener

Next, we need to call the registerlistener() method of sensormanager to register sensoreventlistener to make it effective. The registerlistener() method contains three parameters: sensoreventlistener instance, sensor instance and sensor output information update rate.

The update rate of sensor output information is:

SENSOR_ DELAY_ UI SENSOR_ DELAY_ NORMAL SENSOR_ DELAY_ GAME SENSOR_ DELAY_ FASTEST

A total of four values are optional, and their update rate increases in turn.

The code for registering sensoreventlistener is as follows:

Unregister listener

Finally, when the program exits or the sensor is used up, be sure to call the unregisterlistener() method to release the used resources, as shown below:

Demo example:

Note: there is no need to declare Android manifest permission to develop and use light sensors

In addition, for some programs, the judgment of light sensor can be considered when changing the screen brightness of the current program.

Because most of these programs have such a problem, when the screen brightness is set to a value (in non automatic brightness adjustment mode), it is invalid to set it to automatic brightness adjustment mode.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android view skills, summary of activity operation skills of Android programming, summary of Android operation skills of SQLite database, summary of Android operation skills of JSON format data, summary of Android resource operation skills, and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>