首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >显示Android传感器列表

显示Android传感器列表
EN

Stack Overflow用户
提问于 2010-05-18 22:28:38
回答 4查看 29.1K关注 0票数 23

我正在尝试显示可用传感器的列表,但似乎没有!

我认为这是因为模拟器,但我尝试了在手机上,结果是一样的。

代码语言:javascript
复制
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);
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2858161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档