In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to use light sensors in Android", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use light sensors in Android" article.
Get sensor service
To use the light sensor in Android development, you need to obtain the system sensor service Context.SENSOR_SERVICE first. The method is as follows:
SensorManager senserManager = (SensorManager) getSystemService (Context.SENSOR_SERVICE)
Get the light sensor
SensorManager is the system sensor service and the manager of all sensors in the system. Through it, we get the developed type of sensor, and the method to obtain the light sensor is as follows:
Sensor sensor = senserManager.getDefaultSensor (Sensor.TYPE_LIGHT)
Sensor.TYPE_LIGHT in the code refers to the light sensor.
There are many other sensors in Sensor, which will not be discussed here.
Write listeners
After we have the sensor object, we need to listen to the sensor and use the SensorEventListener interface. There are mainly two methods: onSensorChanged () and onAccuracyChanged (). The code is as follows:
SensorEventListener listener = new SensorEventListener () {@ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {/ / when the accuracy of the sensor changes} @ Override public void onSensorChanged (SensorEvent event) {/ / when the value detected by the sensor changes}}
Register listener
Next, we need to call the registerListener () method of SensorManager to register the SensorEventListener for it to take effect, which takes three parameters: the SensorEventListener instance, the instance of the Sensor, and the update rate of the sensor output.
The update rates of sensor output information are as follows:
SENSOR_DELAY_UI
SENSOR_DELAY_NORMAL
SENSOR_DELAY_GAME
SENSOR_DELAY_FASTEST
A total of four values are available, and their update rates are increasing in turn.
The code to register SensorEventListener is as follows:
SenserManager.registerListener (listener, senser, SensorManager.SENSOR_ DELAY_NORMAL)
Log out of the listener
Finally, when the program exits or the sensor is used, be sure to call the unregisterListener () method to release the resources used, as shown below:
SensorManager.unregisterListener (listener)
DEMO example:
Package com.test.sensor.light;import android.app.Activity;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 Activity {/ / Sensor Manager private SensorManager sensorManager; / / Light brightness private TextView light; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) / / the content of the current Activity is a TextView light = new TextView (this); setContentView (light); / / get the sensor service sensorManager = (SensorManager) getSystemService (Context.SENSOR_SERVICE); / / get the light sensor Sensor sensor = sensorManager.getDefaultSensor (Sensor.TYPE_LIGHT); / / register the listener sensorManager.registerListener (listener, sensor, SensorManager. SENSOR_DELAY_NORMAL);} / / Activity was destroyed @ Override protected void onDestroy () {super.onDestroy (); / / Log out listener if (sensorManager! = null) {sensorManager.unregisterListener (listener) }} / / Sensor event listener private SensorEventListener listener = new SensorEventListener () {/ / when sensor accuracy changes @ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {} / / when the value detected by the sensor changes @ Override public void onSensorChanged (SensorEvent event) {/ / values array the first value is the current light intensity float value = event.values [0] Light.setText ("current brightness" + value + "lx");}};}};} above is about "how to use light sensors in Android". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.