网站建设设计开发公司,四川红叶建设有限公司网站,深圳专业营销网站设计,职业生涯规划大赛是干什么的一、引言
在手机app的开发中经常会用到手机的传感器#xff0c;在《Android App 开发进阶与项目实战》一书的第10章就介绍了传感器的一些功能和用法。要想使用传感器#xff0c;首先得知道手机具备哪些传感器。书中有传感器类型取值的说明#xff0c;并提供了一个查看手机传…一、引言
在手机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 version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.SensorInfoActivityTextViewandroid:idid/tv_titleandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop20dpandroid:text传感器信息android:textSize28spandroid:textStyleboldapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent /ScrollViewandroid:idid/sv_contentandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:layout_marginTop10dpandroid:layout_marginEnd10dpapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/tv_titleLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationvertical TextViewandroid:idid/tv_sensorandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:gravitystartandroid:textColorcolor/blackandroid:textSize15sp //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 MapInteger, String mapSensor new HashMap();Overrideprotected 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);// 获取当前设备支持的传感器列表ListSensor 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.EntryInteger, 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());}
}