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

一、引言

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

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">

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" />

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">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

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" />

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

}

}

雪纳瑞哪里不让摸?
游戏内链接死亡熔炉