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

一、引言

在手机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 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

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());
    }
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武陵悭臾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值