Android獲取設備傳感器的方法
更新時間:2022年09月20日 09:18:45 作者:破z曉
這篇文章主要為大家詳細介紹了Android獲取設備傳感器的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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 {
?
? ? // 動態(tài)申請權限
? ? 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);
? ? ? ? ? ? ? ? //綁定監(jiān)聽器(上下文接口,要監(jiān)聽的傳感器,傳感器采樣率<時間間隔>),返回結果
? ? ? ? ? ? ? ? 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) {
?
? ? ? ? }
?
? ? }
?
?
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android中Fragment的分屏顯示處理橫豎屏顯示的實現方法
今天小編就為大家分享一篇關于Android中Fragment的分屏顯示處理橫豎屏顯示的實現方法,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Android?NDK開發(fā)(C語言--聯合體與枚舉)
這篇文章主要介紹了Android?NDK開發(fā)C語言聯合體與枚舉,共用體是一種特殊的數據類型,允許您在相同的內存位置存儲不同的數據類型。您可以定義一個帶有多成員的共用體,但是任何時候只能有一個成員帶有值。下面詳細介紹該內容,需要的朋友可以參考一下2021-12-12

