欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android獲取設(shè)備傳感器的方法

 更新時間:2022年09月20日 09:18:45   作者:破z曉  
這篇文章主要為大家詳細(xì)介紹了Android獲取設(shè)備傳感器的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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);
? ? }
?
? ? /**
? ? ?* 按鈕點擊事件
? ? ?* */
? ? @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]);//光線強度
? ? ? ? }
? ? ? ? @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ù)方向為正
? ? ? ? ? ? //event.values[1]Y軸加速度,負(fù)方向為正
? ? ? ? ? ? //event.values[2]Z軸加速度,負(fù)方向為正
? ? ? ? ? ? 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軸正方向與地磁北極方向的夾角,順時針方向為正,范圍【0,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)文章

  • Android 打開網(wǎng)絡(luò)上pdf文件

    Android 打開網(wǎng)絡(luò)上pdf文件

    本文主要介紹了Android打開網(wǎng)絡(luò)上pdf文件的方法,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Android自定義控件繪制基本圖形基礎(chǔ)入門

    Android自定義控件繪制基本圖形基礎(chǔ)入門

    這篇文章主要為大家詳細(xì)介紹了Android自定義控件繪制基本圖形基礎(chǔ)入門資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • android實現(xiàn)記住用戶名和密碼以及自動登錄

    android實現(xiàn)記住用戶名和密碼以及自動登錄

    這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)記住用戶名和密碼以及自動登錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Android中模仿抖音加載框之兩顆小球轉(zhuǎn)動效果

    Android中模仿抖音加載框之兩顆小球轉(zhuǎn)動效果

    這篇文章主要介紹了Android仿抖音加載框之兩顆小球轉(zhuǎn)動控件,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • Android自定義View實現(xiàn)QQ運動積分轉(zhuǎn)盤抽獎功能

    Android自定義View實現(xiàn)QQ運動積分轉(zhuǎn)盤抽獎功能

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)QQ運動積分轉(zhuǎn)盤抽獎功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 13問13答全面學(xué)習(xí)Android View繪制

    13問13答全面學(xué)習(xí)Android View繪制

    這篇文章主要為大家詳細(xì)介紹了Android View繪制,13問13答幫助大家全面學(xué)習(xí)Android View繪制,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Android Flutter實現(xiàn)五種酷炫文字動畫效果詳解

    Android Flutter實現(xiàn)五種酷炫文字動畫效果詳解

    animated_text_kit這一動畫庫有多種文字動畫效果,文中將利用它實現(xiàn)五種酷炫的文字動畫:波浪涌動效果、波浪線跳動文字組、彩虹動效、滾動廣告牌效果和打字效果,需要的可以參考一下
    2022-03-03
  • Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法

    Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法

    今天小編就為大家分享一篇關(guān)于Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Android開發(fā)之Activity詳解

    Android開發(fā)之Activity詳解

    本文是翻譯的官方文檔的內(nèi)容,看起來可能會有些生硬,但是內(nèi)容很有用,給大家一個參考,希望對大家學(xué)習(xí)有所幫助。
    2016-06-06
  • Android?NDK開發(fā)(C語言--聯(lián)合體與枚舉)

    Android?NDK開發(fā)(C語言--聯(lián)合體與枚舉)

    這篇文章主要介紹了Android?NDK開發(fā)C語言聯(lián)合體與枚舉,共用體是一種特殊的數(shù)據(jù)類型,允許您在相同的內(nèi)存位置存儲不同的數(shù)據(jù)類型。您可以定義一個帶有多成員的共用體,但是任何時候只能有一個成員帶有值。下面詳細(xì)介紹該內(nèi)容,需要的朋友可以參考一下
    2021-12-12

最新評論