• [置顶] Android代码传感器测试手机支持那几种传感


    一个小小Demo检测手机支持那几种传感:

    具体代码如下:


    [Java代码]DemoSensorActivity.java

    package com.example.testsensor;
    import java.util.List;
    import android.app.Activity;
    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    public class DemoSensorActivity extends Activity {
     private Button button;
     private TextView show;
     private SensorManager sm;
     private StringBuffer str;
     private List<Sensor> allSensors;
     private Sensor s;
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      button = (Button) findViewById(R.id.button);
      show = (TextView) findViewById(R.id.show);
      button.setOnClickListener(new ButtonListener());
      sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
      allSensors = sm.getSensorList(Sensor.TYPE_ALL);// 获得传感器列表
     }
     class ButtonListener implements OnClickListener {
      public void onClick(View v) {
       str = new StringBuffer();
       str.append("该手机有" + allSensors.size() + "个传感器,分别是:\n");
       for (int i = 0; i < allSensors.size(); i++) {
        s = allSensors.get(i);
        str.append("设备名称:" + s.getName() + "\n");
        str.append("设备版本:" + s.getVersion() + "\n");
        str.append("通用类型号:" + s.getType() + "\n");
        str.append("设备商名称:" + s.getVendor() + "\n");
        str.append("传感器功耗:" + s.getPower() + "\n");
        str.append("传感器分辨率:" + s.getResolution() + "\n");
        str.append("传感器最大量程:" + s.getMaximumRange() + "\n");
        switch (s.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
         str.append(i + "加速度传感器");
         break;
        case Sensor.TYPE_GYROSCOPE:
         str.append(i + "陀螺仪传感器");
         break;
        case Sensor.TYPE_LIGHT:
         str.append(i + "环境光线传感器");
         break;
        case Sensor.TYPE_MAGNETIC_FIELD:
         str.append(i + "电磁场传感器");
         break;
        case Sensor.TYPE_ORIENTATION:
         str.append(i + "方向传感器");
         break;
        case Sensor.TYPE_PRESSURE:
         str.append(i + "压力传感器");
         break;
        case Sensor.TYPE_PROXIMITY:
         str.append(i + "距离传感器");
         break;
        case Sensor.TYPE_TEMPERATURE:
         str.append(i + "温度传感器");
         break;
        default:
         str.append(i + "未知传感器");
         break;
        }
       }
       show.setText(str);
      }
     }
    }
    

    [XML代码]main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="获取传感器" />
        <ScrollView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/show"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </ScrollView>
    </LinearLayout>


  • 相关阅读:
    读图,特征提取——形状
    5.2 SW1控制LED1亮灭(中断功能)
    3、寄存器
    5.1、按键SW1控制LED1亮灭
    4.2、LED1、LED2交替闪烁
    2、编程工具IAR、烧写工具SmartRF的使用
    4.1、实现4个LED灯同时闪烁
    1、CC2530单片机介绍
    装windows系统教程
    连接夜神模拟器
  • 原文地址:https://www.cnblogs.com/anjon520/p/3249939.html
Copyright © 2020-2023  润新知