显示一个Android传感器列表传感器、列表、Android

2023-09-04 11:15:19 作者:我是超人我会变身i

我想显示可用传感器的列表,但它像有没有! 我在想,这是因为仿真器,但我想它在手机上,其结果是一样的。

 私人的SensorManager mSensorManager;
TextView的mSensorsTot,mSensorAvailables;

公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    //获取布局和设置为不可见的文本字段
    mSensorsTot =(TextView中)findViewById(R.id.sensoritot);
    mSensorAvailables =(TextView中)findViewById(R.id.sensoridisponibili);

    //获取的SensorManager
    mSensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);

    //可用传感器列表
    名单<传感器> msensorList = mSensorManager.getSensorList(SensorManager.SENSOR_ALL);

    //打印怎么可能传感器是有
    mSensorsTot.setText(msensorList.size()++ this.getString(R.string.sensors)+,!);

    //打印使用sSensList作为字符串中的每个传感器可打印
    字符串sSensList =新的String();
    传感器TMP;
    INT X,I;
    对于(i = 0; I< msensorList.size();我++){
     TMP = msensorList.get(ⅰ);
     sSensList =+ sSensList + tmp.getName(); //添加传感器名称可用传感器串
    }
    //如果有传感器,可显示列表
    如果(ⅰ大于0){
     sSensList =的getString(R.string.sensors)+:+ sSensList;
     mSensorAvailables.setText(sSensList);
    }
}
 

解决方案

恒SensorManager.SENSOR_ALL是德precated,似乎并没有工作了。

查询传感器列表Sensor.TYPE_ALL而不是,它应该工作(我的模拟器返回金鱼3轴加速度)。

I'm trying to display a list of available sensors but it's like there are not! I was thinking that it was because of the emulator, but i tried it on the phone and the result is the same.

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);
    }
}
荷载传感器,维库电子市场网

解决方案

The constant SensorManager.SENSOR_ALL is deprecated and doesn't seem to work anymore.

Query the sensor list with Sensor.TYPE_ALL instead and it should work (my emulator returns a "Goldfish 3-axis Accelerometer").

 
精彩推荐
图片推荐