網站首頁 編程語言 正文
本文實例為大家分享了Android獲取設備傳感器的具體代碼,供大家參考,具體內容如下
結果示例:
xml代碼:
<?xml version="1.0" encoding="utf-8"?> <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" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? tools:context=".MainActivity"> ? ? ? <LinearLayout ? ? ? ? android:id="@+id/liner" ? ? ? ? android:orientation="vertical" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/text" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:hint="text"/> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/accText" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:hint="accText"/> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/luxText" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:hint="luxText"/> ? ? ? </LinearLayout> ? ? ? <GridLayout ? ? ? ? android:id="@+id/gl" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:columnCount="1" ? ? ? ? android:padding="20dp" ? ? ? ? android:rowCount="5" ? ? ? ? android:layout_below="@+id/liner"> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/findAllSensorBut" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="獲取所有傳感器列表" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/lightBut" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_marginTop="10dp" ? ? ? ? ? ? android:text="光線傳感器" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/accelerationBut" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_marginTop="10dp" ? ? ? ? ? ? android:text="加速度傳感器" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/orientationBut" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_marginTop="10dp" ? ? ? ? ? ? android:text="方向傳感器" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/proximityBut" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_marginTop="10dp" ? ? ? ? ? ? android:text="距離傳感器" /> ? ? ? </GridLayout> ? ? ?<LinearLayout ? ? ? ?android:id="@+id/lsLiner" ? ? ? ?android:orientation="vertical" ? ? ? ?android:layout_below="@+id/gl" ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="match_parent"> ? ? ? ? ?<TextView ? ? ? ? ? ?android:text="傳感器列表:" ? ? ? ? ? ?android:layout_width="match_parent" ? ? ? ? ? ?android:layout_height="wrap_content"/> ? ? ? ?<ListView ? ? ? ? ? ?android:id="@+id/lv" ? ? ? ? ? ?android:layout_width="match_parent" ? ? ? ? ? ?android:layout_height="wrap_content"/> ? ? ?</LinearLayout> ? ? </RelativeLayout>
java代碼:
package com.chy.myActivity;
?
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.Manifest;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
?
import java.util.ArrayList;
import java.util.List;
?
?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
?
? ? // 動態申請權限
? ? private String[] permissions = {
? ? ? ? ? ? Manifest.permission.INTERNET,// 網絡權限
? ? ? ? ? ? Manifest.permission.CAMERA,// 相機權限
? ? ? ? ? ? Manifest.permission.RECORD_AUDIO,// 音頻錄制權限
? ? ? ? ? ? Manifest.permission.ACCESS_FINE_LOCATION,// 定位權限
? ? ? ? ? ? Manifest.permission.WRITE_EXTERNAL_STORAGE,// 寫入數據權限
? ? ? ? ? ? Manifest.permission.READ_EXTERNAL_STORAGE,// 讀取數據權限
? ? ? ? ? ? Manifest.permission.ACCESS_COARSE_LOCATION // 獲取基站的服務信號權限,以便獲取位置信息
? ? };
?
?
? ? private Button findAllSensorBut;// 所有傳感器列表
? ? private Button lightBut;// 光線傳感器
? ? private Button accelerationBut;// 加速度傳感器
? ? private Button orientationBut;// 方向傳感器
? ? private Button proximityBut;// 距離傳感器
?
? ? private SensorManager sensorManager;// 傳感器管理器對象
?
? ? private TextView text;
? ? private TextView accText;
? ? private TextView luxText;
? ? private float gravity[] = new float[3];
? ? private float linear_acceleration[] = new float[3];
?
? ? private ListView listView;
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
?
? ? ? ? initView();
? ? ? ? // 獲得傳感器管理器對象
? ? ? ? sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
?
? ? }
?
?
? ? private void initView(){
? ? ? ? text = findViewById(R.id.text);
? ? ? ? accText = findViewById(R.id.accText);
? ? ? ? luxText = findViewById(R.id.luxText);
?
? ? ? ? // 所有傳感器列表
? ? ? ? findAllSensorBut = findViewById(R.id.findAllSensorBut);
? ? ? ? findAllSensorBut.setOnClickListener(this);
? ? ? ? // 光線傳感器
? ? ? ? lightBut = findViewById(R.id.lightBut);
? ? ? ? lightBut.setOnClickListener(this);
? ? ? ? // 加速度傳感器
? ? ? ? accelerationBut = findViewById(R.id.accelerationBut);
? ? ? ? accelerationBut.setOnClickListener(this);
? ? ? ? // 方向傳感器
? ? ? ? orientationBut = findViewById(R.id.orientationBut);
? ? ? ? orientationBut.setOnClickListener(this);
? ? ? ? // 距離傳感器
? ? ? ? proximityBut = findViewById(R.id.proximityBut);
?
? ? ? ? listView = findViewById(R.id.lv);
? ? }
?
? ? /**
? ? ?* 按鈕點擊事件
? ? ?* */
? ? @Override
? ? public void onClick(View v) {
? ? ? ? switch (v.getId()){
? ? ? ? ? ? case R.id.findAllSensorBut:// 傳感器列表
? ? ? ? ? ? ? ? // 數據
? ? ? ? ? ? ? ? ArrayList<String> data = new ArrayList<>();
? ? ? ? ? ? ? ? // 獲取設備中所有傳感器
? ? ? ? ? ? ? ? List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
? ? ? ? ? ? ? ? for (Sensor sensor : sensors) {
? ? ? ? ? ? ? ? ? ? System.out.println("傳感器:"+sensor.getName());
? ? ? ? ? ? ? ? ? ? data.add(sensor.getName());
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
? ? ? ? ? ? ? ? listView.setAdapter(adapter);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.lightBut:// 光線傳感器
? ? ? ? ? ? ? ? //得到默認的光線傳感器
? ? ? ? ? ? ? ? Sensor lightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
? ? ? ? ? ? ? ? //綁定監聽器(上下文接口,要監聽的傳感器,傳感器采樣率<時間間隔>),返回結果
? ? ? ? ? ? ? ? Boolean light_res =sensorManager.registerListener(new LightSensorListener(),lightSensor,SensorManager.SENSOR_DELAY_NORMAL);
? ? ? ? ? ? ? ? Toast.makeText(this,"綁定光線傳感器:"+light_res,Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.accelerationBut:// 加速度傳感器
? ? ? ? ? ? ? ? Sensor accelerometerSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
? ? ? ? ? ? ? ? Boolean acceleration_res=sensorManager.registerListener(new AccerationSensorListener(),accelerometerSensor,SensorManager.SENSOR_DELAY_NORMAL);
? ? ? ? ? ? ? ? Toast.makeText(this,"綁定加速度傳感器:"+acceleration_res,Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.orientationBut:// 方向傳感器
? ? ? ? ? ? ? ? Sensor orientationSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
? ? ? ? ? ? ? ? Boolean orient_res=sensorManager.registerListener(new OrientaationListener(),orientationSensor,SensorManager.SENSOR_DELAY_NORMAL);
? ? ? ? ? ? ? ? Toast.makeText(this,"綁定方向傳感器:"+orient_res,Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.proximityBut:// 距離傳感器
? ? ? ? ? ? ? ? Sensor proximitySensor=sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
? ? ? ? ? ? ? ? Boolean proximity_res=sensorManager.registerListener(new ProximityListener(),proximitySensor,SensorManager.SENSOR_DELAY_NORMAL);
? ? ? ? ? ? ? ? Toast.makeText(this,"綁定距離傳感器:"+proximity_res,Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
?
? ? /**
? ? ?* 光線傳感器類
? ? ?* */
? ?public class LightSensorListener implements SensorEventListener{
? ? ? ? @Override
? ? ? ? //傳感器的數據被打包成event,主要的檢測數據放在enent.values[]數組中
? ? ? ? public void onSensorChanged(SensorEvent event) {
? ? ? ? ? ? System.out.println(event.timestamp);//時間戳
? ? ? ? ? ? System.out.println(event.sensor.getResolution());//分辨率(能識別出最小數值)
? ? ? ? ? ? System.out.println(event.accuracy);//精度(等級)
? ? ? ? ? ? System.out.println(event.values[0]);//光線強度
? ? ? ? }
? ? ? ? @Override
? ? ? ? //傳感器精度變化時調用這個函數
? ? ? ? public void onAccuracyChanged(Sensor sensor, int accuracy) {}
? ? }
?
? ? /**
? ? ?* 加速度傳感器類
? ? ?* */
? ?public class AccerationSensorListener implements SensorEventListener{
? ? ? ? @Override
? ? ? ? public void onSensorChanged(SensorEvent event) {
? ? ? ? ? ? final float alpha=0.8f;
?
? ? ? ? ? ? //event.values[0]X軸加速度,負方向為正
? ? ? ? ? ? //event.values[1]Y軸加速度,負方向為正
? ? ? ? ? ? //event.values[2]Z軸加速度,負方向為正
? ? ? ? ? ? gravity[0]=alpha*gravity[0]+(1-alpha)*event.values[0];
? ? ? ? ? ? gravity[1]=alpha*gravity[1]+(1-alpha)*event.values[1];
? ? ? ? ? ? gravity[2]=alpha*gravity[2]+(1-alpha)*event.values[2];
?
? ? ? ? ? ? linear_acceleration[0]=event.values[0]-gravity[0];
? ? ? ? ? ? linear_acceleration[1]=event.values[1]-gravity[1];
? ? ? ? ? ? linear_acceleration[2]=event.values[2]-gravity[2];
?
? ? ? ? ? ? //通過以上公式可以拋去三個方向上的重力加速度,只剩下純加速度
? ? ? ? ? ? text.setText(linear_acceleration[0] + "");
? ? ? ? ? ? accText.setText(linear_acceleration[1] + "");
? ? ? ? ? ? luxText.setText(linear_acceleration[2] + "");
? ? ? ? }
? ? ? ? @Override
? ? ? ? public void onAccuracyChanged(Sensor sensor, int accuracy) {}
? ? }
?
? ? /**
? ? ?* 方向傳感器類
? ? ?* */
? ? public class OrientaationListener implements SensorEventListener{
? ? ? ? @Override
? ? ? ? public void onSensorChanged(SensorEvent event) {
? ? ? ? ? ? //(需要手機屏幕向上,向下的話南北會反掉)設備繞Z軸旋轉,Y軸正方向與地磁北極方向的夾角,順時針方向為正,范圍【0,180】
? ? ? ? ? ? float azimuth=event.values[0];
? ? ? ? ? ? //設備繞X軸旋轉的角度,當Z軸向Y軸正方向旋轉時為正,反之為負,范圍【-180,180】
? ? ? ? ? ? float pitch=event.values[1];
? ? ? ? ? ? //設備繞Y軸旋轉的角度,當Z軸向X軸正方向旋轉時為負,反之為正,范圍【-90,90】
? ? ? ? ? ? float roll=event.values[2];
?
? ? ? ? ? ? text.setText(azimuth+"");
? ? ? ? ? ? accText.setText(pitch +"");
? ? ? ? ? ? luxText.setText(roll+"");
? ? ? ? }
? ? ? ? @Override
? ? ? ? public void onAccuracyChanged(Sensor sensor, int accuracy) {}
? ? }
?
? ? /**
? ? ?* 距離傳感器類
? ? ?* */
? public class ProximityListener implements SensorEventListener{
?
? ? ? ? @Override
? ? ? ? public void onSensorChanged(SensorEvent event) {
? ? ? ? ? ? //距離傳感器測試手機屏幕距離別的物體的記錄,只有兩個值:0和5
? ? ? ? ? ? //距離很近時為0,否則為5
? ? ? ? ? ? System.out.println(event.values[0]+"");
? ? ? ? }
?
? ? ? ? @Override
? ? ? ? public void onAccuracyChanged(Sensor sensor,int accuracy) {
?
? ? ? ? }
?
? ? }
?
?
}
原文鏈接:https://blog.csdn.net/qq_19688207/article/details/108069750
相關推薦
- 2022-12-08 C/C++高精度運算(大整數運算)詳細講解_C 語言
- 2023-03-02 Kotlin關于協程是什么的探究_Android
- 2021-12-02 Flutter將整個App變為灰色的簡單實現方法_Android
- 2023-03-28 react-native?封裝視頻播放器react-native-video的使用_React
- 2023-02-05 scipy.interpolate插值方法實例講解_python
- 2023-03-01 React?useState的錯誤用法避坑詳解_React
- 2022-09-27 Android?JetPack組件的支持庫Databinding詳解_Android
- 2022-03-28 Python中selenium_webdriver下拉框操作指南_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同步修改后的遠程分支