Check for a sensor on the device

Sensors on the BlackBerry® device that you can work with include the holster sensors, slider sensors, and flip sensors.

  1. Import the required classes and interfaces.
    import net.rim.device.api.system.Sensor;
  2. Invoke Sensor.isSupported() and specify the type of sensor that you want to check for. The sensor types are defined as constants in the Sensor class. The following code sample checks to see whether a device has a slider.
    boolean hasSliderSensor;
    
    hasSliderSensor = Sensor.isSupported(Sensor.SLIDE);
    The method returns true if the device contains the specified sensor, and returns false otherwise.

Code sample: Checking for a sensor on the device

The following code sample checks whether the device has a holster sensor, slider sensor, or flip sensor.

class SensorDemoScreen extends MainScreen
{
    private RichTextField statusField;
    
    public SensorDemoScreen()
    {   
        setTitle("Sensor Demo");
        statusField = new RichTextField();
        add(statusField);
        
        boolean hasHolsterSensor, hasSliderSensor, hasFlipSensor;        

        hasHolsterSensor = Sensor.isSupported(Sensor.HOLSTER);
        hasSliderSensor = Sensor.isSupported(Sensor.SLIDE);
        hasFlipSensor = Sensor.isSupported(Sensor.FLIP);        

        statusField.setText("Holster: " + hasHolsterSensor +
                            "\nSlider: " + hasSliderSensor +
                            "\nFlip: " + hasFlipSensor);
    }
}

Was this information helpful? Send us your comments.