0

我希望在 Actvity 中显示有关传感器的详细信息,但是当我将应用程序放入手机时,我设法仅查看有关加速度计的详细信息,但程序说我有 4 个传感器:加速度计、磁场、方向和温度.

我正在使用 Android 1.6 和 htc Tattoo 进行测试。

这是我的代码: public class SensorInfo extends Activity { private SensorManager mSensorManager; TextView mTextAcc,mTextGyr,mTextLig,mTextMag,mTextOri, mTextPre,mTextPro,mTextTem, mSensorsTotTitle,mSensorAvailablesTitle,mTextAccTitle,mTextGyrTitle,mTextLigTitle,mTextMagTitle,mTextOriTitle, mTextPreTitle,mTextProTitle,mTextTemTitle; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detaillayout); // Get the texts fields of the layout and setup to invisible setTextViews();

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

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

    // Print Sensor Details
    Sensor sens;
    int type,i;
    String text = new String("");
    // Do the list of available sensors on a String and print detail about each sensor
    for (i=0;i<msensorList.size();i++){
        sens = msensorList.get(i);
        type    = sens.getType();
        text = " - "+getString(R.string.power)+" "+String.valueOf(sens.getPower())+"mA\n";
        text+= " - "+getString(R.string.resolution)+" "+String.valueOf(sens.getResolution())+"\n";
        text+= " - "+getString(R.string.maxrange)+" "+String.valueOf(sens.getMaximumRange ())+"\n";
        text+= " - "+getString(R.string.vendor)+" "+sens.getVendor()+"\n";
        text+= " - "+getString(R.string.version)+" "+String.valueOf(sens.getVersion());
        switch(type) {      // Check the type of Sensor that generate the event and show is resolution
        case Sensor.TYPE_ACCELEROMETER:
            mTextAccTitle.setVisibility(0);
            mTextAccTitle.setMaxHeight(30);
            mTextAcc.setVisibility(0);
            mTextAcc.setMaxHeight(100);
            mTextAcc.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_GYROSCOPE:
            mTextGyrTitle.setVisibility(0);
            mTextGyr.setVisibility(0);
            mTextGyr.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_LIGHT:
            mTextLigTitle.setVisibility(0);
            mTextLig.setVisibility(0);
            mTextLig.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:
            mTextMagTitle.setVisibility(0);
            mTextMag.setVisibility(0);
            mTextMag.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_ORIENTATION:
            mTextOriTitle.setVisibility(0);
            mTextOri.setVisibility(0);
            mTextOri.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_PRESSURE:
            mTextPreTitle.setVisibility(0);
            mTextPre.setVisibility(0);
            mTextPre.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_PROXIMITY:
            mTextProTitle.setVisibility(0);
            mTextPro.setVisibility(0);
            mTextPro.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_TEMPERATURE:
            mTextTemTitle.setVisibility(0);
            mTextTem.setVisibility(0);
            mTextTem.setText(text); // Print data of the Sensor
            break;
        }
    }
}
// Get the texts fields of the layout and setup to invisible
void setTextViews(){
    mTextAccTitle       = (TextView) findViewById(R.id.sensorAccTitle);
    mTextAccTitle.setVisibility(4);
    mTextAccTitle.setMaxHeight(0);
    mTextAcc            = (TextView) findViewById(R.id.sensorAcc);
    mTextAcc.setMaxHeight(0);
    mTextAcc.setVisibility(4);
    mTextGyrTitle       = (TextView) findViewById(R.id.sensorGyrTitle);
    mTextGyrTitle.setVisibility(4);
    mTextGyrTitle.setMaxHeight(0);
    mTextGyr            = (TextView) findViewById(R.id.sensorGyr);
    mTextGyr.setVisibility(4);
    mTextGyrTitle.setMaxHeight(0);
    mTextLigTitle       = (TextView) findViewById(R.id.sensorLigTitle);
    mTextLigTitle.setVisibility(4);
    mTextLigTitle.setMaxHeight(0);
    mTextLig            = (TextView) findViewById(R.id.sensorLig);
    mTextLig.setVisibility(4);
    mTextLig.setMaxHeight(0);
    mTextMagTitle       = (TextView) findViewById(R.id.sensorMagTitle);
    mTextMagTitle.setVisibility(4);
    mTextMagTitle.setMaxHeight(0);
    mTextMag            = (TextView) findViewById(R.id.sensorMag);
    mTextMag.setVisibility(4);
    mTextMag.setMaxHeight(0);
    mTextOriTitle       = (TextView) findViewById(R.id.sensorOriTitle);
    mTextOriTitle.setVisibility(4);
    mTextOriTitle.setMaxHeight(0);
    mTextOri            = (TextView) findViewById(R.id.sensorOri);
    mTextOri.setVisibility(4);
    mTextOri.setMaxHeight(0);
    mTextPreTitle       = (TextView) findViewById(R.id.sensorPreTitle);
    mTextPreTitle.setVisibility(4);
    mTextPreTitle.setMaxHeight(0);
    mTextPre            = (TextView) findViewById(R.id.sensorPre);
    mTextPre.setVisibility(4);
    mTextPre.setMaxHeight(0);
    mTextProTitle       = (TextView) findViewById(R.id.sensorProTitle);
    mTextProTitle.setVisibility(4);
    mTextProTitle.setMaxHeight(0);
    mTextPro            = (TextView) findViewById(R.id.sensorPro);
    mTextPro.setVisibility(4);
    mTextPro.setMaxHeight(0);
    mTextTemTitle       = (TextView) findViewById(R.id.sensorTemTitle);
    mTextTemTitle.setVisibility(4);
    mTextTemTitle.setMaxHeight(0);
    mTextTem            = (TextView) findViewById(R.id.sensorTem);
    mTextTem.setVisibility(4);
    mTextTem.setMaxHeight(0);
}

}

来自意大利的坦克瓦莱里奥

4

1 回答 1

3

已解决,我只将默认传感器注册到 de sensorListener,而我必须分别注册每个传感器!

这是最终代码:

public class SensorInfo extends Activity {
private SensorManager mSensorManager;
TextView mTextAcc,mTextGyr,mTextLig,mTextMag,mTextOri,
 mTextPre,mTextPro,mTextTem,
 mSensorsTotTitle,mSensorAvailablesTitle,mTextAccTitle,mTextGyrTitle,mTextLigTitle,mTextMagTitle,mTextOriTitle,
 mTextPreTitle,mTextProTitle,mTextTemTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detaillayout);
    // Get the texts fields of the layout and setup to invisible
    setTextViews();

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

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

    // Print Sensor Details
    Sensor sens;
    int type,i;
    String text = new String("");
    // Do the list of available sensors on a String and print detail about each sensor
    for (i=0;i<msensorList.size();i++){
        sens = msensorList.get(i);
        type    = sens.getType();
        text = " - "+getString(R.string.power)+" "+String.valueOf(sens.getPower())+"mA\n";
        text+= " - "+getString(R.string.resolution)+" "+String.valueOf(sens.getResolution())+"\n";
        text+= " - "+getString(R.string.maxrange)+" "+String.valueOf(sens.getMaximumRange ())+"\n";
        text+= " - "+getString(R.string.vendor)+" "+sens.getVendor()+"\n";
        text+= " - "+getString(R.string.version)+" "+String.valueOf(sens.getVersion());
        switch(type) {      // Check the type of Sensor that generate the event and show is resolution
        case Sensor.TYPE_ACCELEROMETER:
            mTextAccTitle.setVisibility(0);
            mTextAccTitle.setMaxHeight(30);
            mTextAcc.setVisibility(0);
            mTextAcc.setMaxHeight(100);
            mTextAcc.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_GYROSCOPE:
            mTextGyrTitle.setVisibility(0);
            mTextGyrTitle.setMaxHeight(30);
            mTextGyr.setVisibility(0);
            mTextGyr.setMaxHeight(100);
            mTextGyr.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_LIGHT:
            mTextLigTitle.setVisibility(0);
            mTextLigTitle.setMaxHeight(30);
            mTextLig.setVisibility(0);
            mTextLig.setMaxHeight(100);
            mTextLig.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:
            mTextMagTitle.setVisibility(0);
            mTextMagTitle.setMaxHeight(30);
            mTextMag.setVisibility(0);
            mTextMag.setMaxHeight(100);
            mTextMag.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_ORIENTATION:
            mTextOriTitle.setVisibility(0);
            mTextOriTitle.setMaxHeight(30);
            mTextOri.setVisibility(0);
            mTextOri.setMaxHeight(100);
            mTextOri.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_PRESSURE:
            mTextPreTitle.setVisibility(0);
            mTextPreTitle.setMaxHeight(30);
            mTextPre.setVisibility(0);
            mTextPre.setMaxHeight(100);
            mTextPre.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_PROXIMITY:
            mTextProTitle.setVisibility(0);
            mTextProTitle.setMaxHeight(30);
            mTextPro.setVisibility(0);
            mTextPro.setMaxHeight(100);
            mTextPro.setText(text); // Print data of the Sensor
            break;
        case Sensor.TYPE_TEMPERATURE:
            mTextTemTitle.setVisibility(0);
            mTextTemTitle.setMaxHeight(30);
            mTextTem.setVisibility(0);
            mTextTem.setMaxHeight(100);
            mTextTem.setText(text); // Print data of the Sensor
            break;
        }
    }
}
// Get the texts fields of the layout and setup to invisible
void setTextViews(){
    mTextAccTitle       = (TextView) findViewById(R.id.sensorAccTitle);
    mTextAccTitle.setVisibility(4);
    mTextAccTitle.setMaxHeight(0);
    mTextAcc            = (TextView) findViewById(R.id.sensorAcc);
    mTextAcc.setMaxHeight(0);
    mTextAcc.setVisibility(4);
    mTextGyrTitle       = (TextView) findViewById(R.id.sensorGyrTitle);
    mTextGyrTitle.setVisibility(4);
    mTextGyrTitle.setMaxHeight(0);
    mTextGyr            = (TextView) findViewById(R.id.sensorGyr);
    mTextGyr.setVisibility(4);
    mTextGyrTitle.setMaxHeight(0);
    mTextLigTitle       = (TextView) findViewById(R.id.sensorLigTitle);
    mTextLigTitle.setVisibility(4);
    mTextLigTitle.setMaxHeight(0);
    mTextLig            = (TextView) findViewById(R.id.sensorLig);
    mTextLig.setVisibility(4);
    mTextLig.setMaxHeight(0);
    mTextMagTitle       = (TextView) findViewById(R.id.sensorMagTitle);
    mTextMagTitle.setVisibility(4);
    mTextMagTitle.setMaxHeight(0);
    mTextMag            = (TextView) findViewById(R.id.sensorMag);
    mTextMag.setVisibility(4);
    mTextMag.setMaxHeight(0);
    mTextOriTitle       = (TextView) findViewById(R.id.sensorOriTitle);
    mTextOriTitle.setVisibility(4);
    mTextOriTitle.setMaxHeight(0);
    mTextOri            = (TextView) findViewById(R.id.sensorOri);
    mTextOri.setVisibility(4);
    mTextOri.setMaxHeight(0);
    mTextPreTitle       = (TextView) findViewById(R.id.sensorPreTitle);
    mTextPreTitle.setVisibility(4);
    mTextPreTitle.setMaxHeight(0);
    mTextPre            = (TextView) findViewById(R.id.sensorPre);
    mTextPre.setVisibility(4);
    mTextPre.setMaxHeight(0);
    mTextProTitle       = (TextView) findViewById(R.id.sensorProTitle);
    mTextProTitle.setVisibility(4);
    mTextProTitle.setMaxHeight(0);
    mTextPro            = (TextView) findViewById(R.id.sensorPro);
    mTextPro.setVisibility(4);
    mTextPro.setMaxHeight(0);
    mTextTemTitle       = (TextView) findViewById(R.id.sensorTemTitle);
    mTextTemTitle.setVisibility(4);
    mTextTemTitle.setMaxHeight(0);
    mTextTem            = (TextView) findViewById(R.id.sensorTem);
    mTextTem.setVisibility(4);
    mTextTem.setMaxHeight(0);
}

}

再见瓦莱里奥

于 2010-11-23T17:34:16.843 回答