开发者

Problem Showing Sensors Details

i'm looking to show detail about sensors in an Actvity but when i put my app in to my phone i manage to view only details about the accellerometer, but the program says that i have 4 sensors: Accellerometer, Magnetic field, Orientation and Temperature.

I'm using Android 1.6 and a htc Tattoo for testing.

This is my code: 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开发者_JAVA技巧(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);
}

}

Tank's Valerio From Italy


Resolved, I was registering only the default sensor to de sensorListener while i had to register each sensor separately!

Here is the final code:

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);
}

}

Bye Valerio

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜