網站首頁 編程語言 正文
本文實例為大家分享了Android光線傳感器使用的具體代碼,供大家參考,具體內容如下
一、首先是布局頁面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>
二、在對應的Activity中獲取光線傳感器的值LightSensorActivity,具體注釋已經在代碼中給出
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();
? ? ? ? //第一個參數:SensorEventListener對象用this來指定就可以了
? ? ? ? // 第二個參數:傳感器對象 光線傳感器類型的常量:TYPE_LIGHT
? ? ? ? // 第三個參數:傳感器數據的頻率 這里采用適合游戲的頻率
? ? ? ? sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_GAME);
? ? }
? ? @Override
? ? protected void onPause() {
? ? ? ? super.onPause();
? ? ? ? sensorManager.unregisterListener(this);
? ? }
? ? //當傳感器的值,發生變化時,回調的方法
? ? @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());
? ? ? ? }
? ? }
? ? //當傳感器的精度,發生變化時,回調的方法
? ? @Override
? ? public void onAccuracyChanged(Sensor sensor, int accuracy) {
? ? }
}
效果如圖所示:
以上是光線傳感器的簡單使用。
原文鏈接:https://blog.csdn.net/lu202032/article/details/122029687
相關推薦
- 2023-10-16 element組件autofocus( 自動獲取焦點)失效
- 2022-06-16 Python數據結構之遞歸方法詳解_python
- 2022-10-14 nginx和apache的區別
- 2022-05-31 .NET?MAUI項目中創建超鏈接_實用技巧
- 2022-08-17 C語言超全面講解字符串函數_C 語言
- 2022-02-28 CommonsChunkPlugin 插件使用方法 、 出現報錯 : Error: webpa
- 2023-07-07 什么是依賴注入?可以通過多少種方式完成依賴注入?
- 2022-08-24 Python?常見的配置文件寫法梳理匯總_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支