Android獲取設(shè)備傳感器的方法
本文實(shí)例為大家分享了Android獲取設(shè)備傳感器的具體代碼,供大家參考,具體內(nèi)容如下
結(jié)果示例:

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)申請權(quán)限
? ? private String[] permissions = {
? ? ? ? ? ? Manifest.permission.INTERNET,// 網(wǎng)絡(luò)權(quán)限
? ? ? ? ? ? Manifest.permission.CAMERA,// 相機(jī)權(quán)限
? ? ? ? ? ? Manifest.permission.RECORD_AUDIO,// 音頻錄制權(quán)限
? ? ? ? ? ? Manifest.permission.ACCESS_FINE_LOCATION,// 定位權(quán)限
? ? ? ? ? ? Manifest.permission.WRITE_EXTERNAL_STORAGE,// 寫入數(shù)據(jù)權(quán)限
? ? ? ? ? ? Manifest.permission.READ_EXTERNAL_STORAGE,// 讀取數(shù)據(jù)權(quán)限
? ? ? ? ? ? Manifest.permission.ACCESS_COARSE_LOCATION // 獲取基站的服務(wù)信號權(quán)限,以便獲取位置信息
? ? };
?
?
? ? 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);
? ? }
?
? ? /**
? ? ?* 按鈕點(diǎn)擊事件
? ? ?* */
? ? @Override
? ? public void onClick(View v) {
? ? ? ? switch (v.getId()){
? ? ? ? ? ? case R.id.findAllSensorBut:// 傳感器列表
? ? ? ? ? ? ? ? // 數(shù)據(jù)
? ? ? ? ? ? ? ? ArrayList<String> data = new ArrayList<>();
? ? ? ? ? ? ? ? // 獲取設(shè)備中所有傳感器
? ? ? ? ? ? ? ? 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:// 光線傳感器
? ? ? ? ? ? ? ? //得到默認(rèn)的光線傳感器
? ? ? ? ? ? ? ? Sensor lightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
? ? ? ? ? ? ? ? //綁定監(jiān)聽器(上下文接口,要監(jiān)聽的傳感器,傳感器采樣率<時間間隔>),返回結(jié)果
? ? ? ? ? ? ? ? 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
? ? ? ? //傳感器的數(shù)據(jù)被打包成event,主要的檢測數(shù)據(jù)放在enent.values[]數(shù)組中
? ? ? ? public void onSensorChanged(SensorEvent event) {
? ? ? ? ? ? System.out.println(event.timestamp);//時間戳
? ? ? ? ? ? System.out.println(event.sensor.getResolution());//分辨率(能識別出最小數(shù)值)
? ? ? ? ? ? System.out.println(event.accuracy);//精度(等級)
? ? ? ? ? ? System.out.println(event.values[0]);//光線強(qiáng)度
? ? ? ? }
? ? ? ? @Override
? ? ? ? //傳感器精度變化時調(diào)用這個函數(shù)
? ? ? ? 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軸加速度,負(fù)方向?yàn)檎?
? ? ? ? ? ? //event.values[1]Y軸加速度,負(fù)方向?yàn)檎?
? ? ? ? ? ? //event.values[2]Z軸加速度,負(fù)方向?yàn)檎?
? ? ? ? ? ? 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) {
? ? ? ? ? ? //(需要手機(jī)屏幕向上,向下的話南北會反掉)設(shè)備繞Z軸旋轉(zhuǎn),Y軸正方向與地磁北極方向的夾角,順時針方向?yàn)檎?,范圍?,180】
? ? ? ? ? ? float azimuth=event.values[0];
? ? ? ? ? ? //設(shè)備繞X軸旋轉(zhuǎn)的角度,當(dāng)Z軸向Y軸正方向旋轉(zhuǎn)時為正,反之為負(fù),范圍【-180,180】
? ? ? ? ? ? float pitch=event.values[1];
? ? ? ? ? ? //設(shè)備繞Y軸旋轉(zhuǎn)的角度,當(dāng)Z軸向X軸正方向旋轉(zhuǎn)時為負(fù),反之為正,范圍【-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) {
? ? ? ? ? ? //距離傳感器測試手機(jī)屏幕距離別的物體的記錄,只有兩個值:0和5
? ? ? ? ? ? //距離很近時為0,否則為5
? ? ? ? ? ? System.out.println(event.values[0]+"");
? ? ? ? }
?
? ? ? ? @Override
? ? ? ? public void onAccuracyChanged(Sensor sensor,int accuracy) {
?
? ? ? ? }
?
? ? }
?
?
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Cocos2d-x入門教程(詳細(xì)的實(shí)例和講解)
這篇文章主要介紹了Cocos2d-x入門教程,包括詳細(xì)的實(shí)例、講解以及實(shí)現(xiàn)過程,需要的朋友可以參考下2014-04-04
Android Studio 4.0 新功能中的Live Layout Inspector詳解
這篇文章主要介紹了Android Studio 4.0 新功能中的Live Layout Inspector,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
Android自定義WaveView實(shí)現(xiàn)波浪進(jìn)度效果
最近注意到百度外賣以及淘寶個人中心,都用到了類似水波起伏的效果,于是就參照網(wǎng)上的資料然后自己整改,自定義了一個waveView來實(shí)現(xiàn)這個效果,文中給出來詳細(xì)的實(shí)現(xiàn)原理及實(shí)例代碼,有需要的朋友們可以參考借鑒,下面來一起看看吧。2017-01-01
android studio 3.6.1導(dǎo)入項(xiàng)目報錯提示無法下載classpath里的內(nèi)容
這篇文章主要介紹了android studio 3.6.1導(dǎo)入項(xiàng)目報錯提示無法下載classpath里的內(nèi)容,本文通過原因分析通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Android網(wǎng)絡(luò)請求-sign參數(shù)的設(shè)置方式
這篇文章主要介紹了Android網(wǎng)絡(luò)請求-sign參數(shù)的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android開發(fā)之獲取LayoutInflater對象的方法總結(jié)
這篇文章主要介紹了Android開發(fā)之獲取LayoutInflater對象的方法,結(jié)合實(shí)例形式總結(jié)分析了Android獲取LayoutInflater對象的常用技巧,需要的朋友可以參考下2016-02-02
Flutter倒計(jì)時/計(jì)時器的實(shí)現(xiàn)代碼
這篇文章主要介紹了Flutter倒計(jì)時/計(jì)時器的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Android實(shí)現(xiàn)簡潔的APP更新dialog數(shù)字進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡潔的APP更新dialog數(shù)字進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Android天氣預(yù)報之基于HttpGet對象解析天氣數(shù)據(jù)的方法
這篇文章主要介紹了Android天氣預(yù)報之基于HttpGet對象解析天氣數(shù)據(jù)的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08

