android - 显示 Android 传感器列表

标签 android sensors

我正在尝试显示可用传感器的列表,但好像没有!
我还以为是模拟器的原因,结果在手机上试了一下,结果是一样的。

private SensorManager mSensorManager;
TextView mSensorsTot,mSensorAvailables;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Get the texts fields of the layout and setup to invisible
    mSensorsTot   = (TextView) findViewById(R.id.sensoritot);
    mSensorAvailables  = (TextView) findViewById(R.id.sensoridisponibili);

    // Get the SensorManager 
    mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);

    // List of Sensors Available
    List<Sensor> msensorList = mSensorManager.getSensorList(SensorManager.SENSOR_ALL);

    // Print how may Sensors are there
    mSensorsTot.setText(msensorList.size()+" "+this.getString(R.string.sensors)+"!");

    // Print each Sensor available using sSensList as the String to be printed
    String sSensList = new String("");
    Sensor tmp;
    int x,i;
    for (i=0;i<msensorList.size();i++){
     tmp = msensorList.get(i);
     sSensList = " "+sSensList+tmp.getName(); // Add the sensor name to the string of sensors available
    }
    // if there are sensors available show the list
    if (i>0){
     sSensList = getString(R.string.sensors)+":"+sSensList;
     mSensorAvailables.setText(sSensList);
    }
}

最佳答案

常数 SensorManager.SENSOR_ALL已弃用,似乎不再起作用。

使用 Sensor.TYPE_ALL 查询传感器列表相反,它应该可以工作(我的模拟器返回“Goldfish 3-axis Accelerometer”)。

关于android - 显示 Android 传感器列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2858161/

相关文章:

java - GPS 提供商区分室内和室外

java - HashMap 会通过字符串类型提供对对象数组的有效访问吗?

python - 如何在Python中根据用户输入运行各种温度读数?

CCS/MSP430FR6989 : Capture/Compare Interrupt not working with HR-S04 Ultrasonic Sensor

不同设备的 Android 方向传感器不同?

android - 使用静态产品测试计费返回 SERVICE_DISCONNECTED

android - Gingerbread 模拟器实例比 Froyo 及以下版本更慢。为什么?

android - 如何在 ListView 中显示项目列表

c - µs-precision wait in C for linux that does not put program to sleep?

android - 使用 Android 加速度计检测振动