iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android获取设备传感器的方法
  • 851
分享到

Android获取设备传感器的方法

2024-04-02 19:04:59 851人浏览 独家记忆
摘要

本文实例为大家分享了Android获取设备传感器的具体代码,供大家参考,具体内容如下 结果示例: xml代码: <?xml version="1.0" encoding="u

本文实例为大家分享了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) {
 
        }
 
    }
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Android获取设备传感器的方法

本文链接: https://www.lsjlt.com/news/167930.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android获取设备传感器的方法
    本文实例为大家分享了Android获取设备传感器的具体代码,供大家参考,具体内容如下 结果示例: xml代码: <xml version="1.0" encoding="ut...
    99+
    2024-04-02
  • Android传感器数据获取的方法
    本文实例为大家分享了Android传感器数据获取的具体代码,供大家参考,具体内容如下 接着上篇wifi列表获取,我们这篇接着说传感器。还是看代码 我们先自己定义一个传感器的工具类(因...
    99+
    2024-04-02
  • Android 如何获取传感器的数据方法详解
    目录1 传感器简介2 传感器的使用2.1 获取传感器服务2.2 获取待监听的传感器2.3 注册传感器的监听器2.4 注销传感器的监听器3 示例代码1 传感器简介 传感器 Sensor...
    99+
    2024-04-02
  • 获取Android设备序列号的方法
    获取Android设备序列号的方法 在Android应用开发中,有时我们需要获取设备的唯一标识,例如设备序列号。设备序列号是一个字符串,可以用于标识特定的Android设备。本文将介绍如何在Andro...
    99+
    2023-10-11
    android Android
  • Android开发如何获取传感器数据
    这篇文章主要介绍了Android开发如何获取传感器数据,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。本文实例讲述了Android开发获取传感器数据的方法。分享给大家供大家参考...
    99+
    2023-05-30
    android
  • Android获取蓝牙设备列表的方法
    最近换了一家公司,主要内容是做关于移动端室内定位的相关sdk,刚进来的时候信心满满,誓要干出一番天地!!!结果进来快一个多月了,根本没轮到我施展拳脚,给我搁置在一旁自己弄自己的。行吧...
    99+
    2024-04-02
  • Android应用获取设备序列号的方法
    软硬件环境 Macbook Pro MGX 72 Android studio 2.1.2 Android 5.1.1前言上一篇介绍了如何获取ethernet的MAC地址,对于厂商来讲,除了MAC号,还有一项数据也很重要,那就是机器序...
    99+
    2023-05-31
    android 设备 序列号
  • Android传感器的简单使用方法
    本文实例为大家分享了Android传感器简单使用的具体代码,供大家参考,具体内容如下 1. SensorManager类 SensorManager类用来管理各个传感器:通过Sens...
    99+
    2024-04-02
  • Android 获取传感器列表整理及简单实例
    Android 获取传感器列表整理及简单实例Android 4.4 (API等级19)支持以下传感器:   TYPE_ACCELEROMETER 加速度传感器,单位是m/s2,测量应用于设备X、Y、Z轴上的加速度 ...
    99+
    2023-05-31
    android 传感器 roi
  • Android 7.0开发获取存储设备信息的方法
    本文实例讲述了 Android 7.0开发获取存储设备信息的方法。分享给大家供大家参考,具体如下:Android 7.0开发相较之前有不少改进,具体可参考前面的文章Android7.0版本影响开发的改进分析,这里简单总结一下Android ...
    99+
    2023-05-30
    android7.0 存储设备 roi
  • Android 获取设备屏幕大小的几种方法总结
    1、通过WindowManager获取DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);System...
    99+
    2023-05-31
    android 获取屏幕 roi
  • Android中的传感器
    Android系统提供了对传感器的支持,手机硬件如哦提供了这些传感器,则Android应用可以通过传感器来获取设备外界条件,包括手机的运行状态、外界磁场、温度压力等。Android系统提供了驱动程序来管理这些传感器硬件,对于开发者来说,只要...
    99+
    2023-08-31
    android android studio ide Powered by 金山文档
  • Android应用中实现如何获取所有传感器数据的
    这篇文章给大家介绍Android应用中实现如何获取所有传感器数据的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。main.xml<&#63;xml version="1.0" enco...
    99+
    2023-05-31
    android roi
  • Android应用中获取设备唯一ID的方法有哪些
    本篇文章为大家展示了Android应用中获取设备唯一ID的方法有哪些,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。先来看看几种比较单一的方式:IMEI方式:TelephonyManager.getD...
    99+
    2023-05-31
    android roi
  • Android光线传感器使用方法详解
    本文实例为大家分享了Android光线传感器使用的具体代码,供大家参考,具体内容如下 一、首先是布局页面activity_light_sensor.xml <xml versi...
    99+
    2024-04-02
  • Android采集传感器数据并显示的方法
    本文实例为大家分享了Android采集传感器数据并显示的具体代码,供大家参考,具体内容如下 需要的知识 Android 项目主配置文件 AndroidManifest.xm...
    99+
    2024-04-02
  • Android编程如何使用光线传感器获取光线强弱
    这篇文章将为大家详细讲解有关Android编程如何使用光线传感器获取光线强弱,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。本文实例讲述了Android编程使用光线传感器获取光线强弱的方法。分享给大家供大家...
    99+
    2023-05-30
    android
  • android如何获取设备唯一标识
    这篇文章主要介绍了android如何获取设备唯一标识,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体如下: public static Str...
    99+
    2023-05-30
    android
  • Android 如何获取设备唯一标识
    目录一. 先简单总结一下比较常见的几个解决方案的弊端:1. IMEI2. Android ID3. MAC地址二. uuid + 本地文件,实现一个通用解决方案1. 思路2. 解决手...
    99+
    2024-04-02
  • Android设备序列号:如何获取和查找您的Android设备的序列号
    Android设备序列号:如何获取和查找您的Android设备的序列号 在使用Android设备时,有时候需要查找设备的序列号来进行诊断、维修或者进行其他操作。本文将介绍如何获取和查找Android设...
    99+
    2023-10-01
    android Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作