網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android光線傳感器使用的具體代碼,供大家參考,具體內(nèi)容如下
一、首先是布局頁面activity_light_sensor.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" ? ? android:orientation="vertical" ? ? tools:context=".LightSensorActivity"> ? ? <TextView ? ? ? ? android:id="@+id/textView" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="60dp" ? ? ? ? android:gravity="center" ? ? ? ? android:text="光線傳感器" ? ? ? ? android:textColor="@color/black" ? ? ? ? android:textSize="20sp" /> ? ? <EditText ? ? ? ? android:id="@+id/editText" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" /> </LinearLayout>
二、在對應(yīng)的Activity中獲取光線傳感器的值LightSensorActivity,具體注釋已經(jīng)在代碼中給出
public class LightSensorActivity extends AppCompatActivity implements SensorEventListener {
? ? private EditText editText;
? ? //傳感器管理器對象
? ? private SensorManager sensorManager;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_light_sensor);
? ? ? ? editText = findViewById(R.id.editText);
? ? ? ? sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
? ? }
? ? @Override
? ? protected void onResume() {
? ? ? ? super.onResume();
? ? ? ? //第一個參數(shù):SensorEventListener對象用this來指定就可以了
? ? ? ? // 第二個參數(shù):傳感器對象 光線傳感器類型的常量:TYPE_LIGHT
? ? ? ? // 第三個參數(shù):傳感器數(shù)據(jù)的頻率 這里采用適合游戲的頻率
? ? ? ? sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_GAME);
? ? }
? ? @Override
? ? protected void onPause() {
? ? ? ? super.onPause();
? ? ? ? sensorManager.unregisterListener(this);
? ? }
? ? //當(dāng)傳感器的值,發(fā)生變化時,回調(diào)的方法
? ? @Override
? ? public void onSensorChanged(SensorEvent event) {
? ? ? ? //獲取傳感器的值
? ? ? ? float[] values= event.values;
? ? ? ? //獲取傳感器類型
? ? ? ? int sensorType = event.sensor.getType();
? ? ? ? StringBuilder stringBuilder = null;
? ? ? ? if (sensorType==Sensor.TYPE_LIGHT){
? ? ? ? ? ? stringBuilder = new StringBuilder();
? ? ? ? ? ? stringBuilder.append("光的強度值:");
? ? ? ? ? ? //添加獲取的傳感器的值
? ? ? ? ? ? stringBuilder.append(values[0]);
? ? ? ? ? ? editText.setText(stringBuilder.toString());
? ? ? ? }
? ? }
? ? //當(dāng)傳感器的精度,發(fā)生變化時,回調(diào)的方法
? ? @Override
? ? public void onAccuracyChanged(Sensor sensor, int accuracy) {
? ? }
}
效果如圖所示:
以上是光線傳感器的簡單使用。
原文鏈接:https://blog.csdn.net/lu202032/article/details/122029687
相關(guān)推薦
- 2023-01-10 Qt實現(xiàn)可以計算大數(shù)的簡單計算器_C 語言
- 2022-04-09 python的函數(shù)參數(shù)你了解嗎_python
- 2022-06-06 Kafka在Zookeeper中存儲的信息有哪些?
- 2022-04-09 SpringBoot 安全漏洞之SQL注入解決方案
- 2024-07-18 restTemplate使用總結(jié)
- 2022-12-05 Python最長回文子串問題_python
- 2022-09-25 解決ERROR in Conflict: Multiple assets emit differen
- 2022-08-03 GoFrame?ORM原生方法操作示例_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支