安卓应用开发学习:查看手机传感器信息

一、引言

在手机app的开发中经常会用到手机的传感器,在《Android App 开发进阶与项目实战》一书的第10章就介绍了传感器的一些功能和用法。要想使用传感器,首先得知道手机具备哪些传感器。书中有传感器类型取值的说明,并提供了一个查看手机传感器的的示例代码,这次我就直接拿来用了。

二、传感器种类

关于传感器的种类在《Android App 开发进阶与项目实战》一书中有介绍,我就懒得码字了。

Android系统提供了传感管理器SensorManager统一管理各类传感器,其对象从系统服务SENSOR_SERVICE中获取。要查看当前设备支持的传感器种类,可通过传感器管理对象的getSensorList方法获得,该方法返回一个Sensor列表。遍历Sensor列表中的每个元素得到感应器对象Sensor,再调用Sensor对象的getType方法可获取该传感器的类型,调用Sensor对象的getName方法可获得该传感器的名称。

我照着书中的示例代码做了个应用,在自己的手机上运行,得到了以下的结果。我的OPPO手机支持19种传感器。以后可以针对这19种传感器,学习相应的应用开发啦。

三、代码展示

最终的代码如下:

  1. 界面设计文件 activity_sensor_info.xml
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SensorInfoActivity">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="传感器信息"
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:id="@+id/sv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_sensor"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:textColor="@color/black"
                android:textSize="15sp" />
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

2.逻辑代码 SensorInfoActivity.java

java 复制代码
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class SensorInfoActivity extends AppCompatActivity {
    private final static String TAG = "SensorInfoActivity";
    private TextView tv_sensor; // 声明一个文本视图对象
    private final String[] mSensorType = {
            "加速度", "磁场", "方向", "陀螺仪", "光线",
            "压力", "温度", "距离", "重力", "线性加速度",
            "旋转矢量", "湿度", "环境温度", "无标定磁场", "无标定旋转矢量",
            "未校准陀螺仪", "特殊动作", "步行检测", "计步器", "地磁旋转矢量",
            "心跳速率", "倾斜检测", "唤醒手势", "掠过手势", "拾起手势",
            "手腕倾斜", "设备方向", "六自由度姿态", "静止检测", "运动检测",
            "心跳检测", "动态元事件", "未知", "低延迟离体检测", "低延迟体外检测"};
    private Map<Integer, String> mapSensor = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor_info);
        tv_sensor = findViewById(R.id.tv_sensor);
        showSensorInfo(); // 显示手机自带的传感器信息
    }

    // 显示手机自带的传感器信息
    private void showSensorInfo() {
        // 从系统服务中获取传感管理器对象
        SensorManager mSensorMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        // 获取当前设备支持的传感器列表
        List<Sensor> sensorList = mSensorMgr.getSensorList(Sensor.TYPE_ALL);
        StringBuilder show_content = new StringBuilder("当前支持的传感器包括:\n");
        for (Sensor sensor : sensorList) {
            if (sensor.getType() >= mSensorType.length) {
                continue;
            }
            mapSensor.put(sensor.getType(), sensor.getName());
        }
        for (Map.Entry<Integer, String> map : mapSensor.entrySet()) {
            int type = map.getKey();
            String name = map.getValue();
            String content = String.format(Locale.CHINESE,"%d %s:%s\n", type, mSensorType[type - 1], name);
            show_content.append(content);
        }
        tv_sensor.setText(show_content.toString());
    }
}
相关推荐
平凡シンプル13 分钟前
安卓 uniapp跨端开发
android·uni-app
elina801316 分钟前
安卓实现导入Excel文件
android·excel
严文文-Chris20 分钟前
【设计模式-享元】
android·java·设计模式
微刻时光21 分钟前
Redis集群知识及实战
数据库·redis·笔记·学习·程序人生·缓存
AORO_BEIDOU36 分钟前
防爆手机+鸿蒙系统,遨游通讯筑牢工业安全基石
5g·安全·智能手机·信息与通信·harmonyos
Auv开心37 分钟前
【手机马达共振导致后主摄马达声音异常】
智能手机
趋势大仙1 小时前
SQLiteDatabase insert or replace数据不生效
android·数据库
DS小龙哥1 小时前
QT For Android开发-打开PPT文件
android·qt·powerpoint
试行2 小时前
Android实现自定义下拉列表绑定数据
android·java
茜茜西西CeCe2 小时前
移动技术开发:简单计算器界面
java·gitee·安卓·android-studio·移动技术开发·原生安卓开发