Code sample: Working with a sensor

import net.rim.device.api.system.*;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

public class SensorDemo extends UiApplication 
{
    public SensorDemo() 
    {
        SensorDemoScreen screen = new SensorDemoScreen();
        pushScreen(screen);
    }
    public static void main(String[] args) 
    {
        SensorDemo app = new SensorDemo();
        app.enterEventDispatcher();
    }
    class SensorDemoScreen extends MainScreen implements SensorListener 
    {
        private RichTextField statusField1;
        private RichTextField statusField2;

        public SensorDemoScreen() 
        {
            setTitle("Sensor Demo");
            statusField1 = new RichTextField();
            statusField2 = new RichTextField();
            add(statusField1);
            add(statusField2);

            // Check for the presence of a sensor
            boolean hasFlipSensor, hasHolsterSensor, hasSliderSensor;
            hasFlipSensor = Sensor.isSupported(Sensor.FLIP);
            hasHolsterSensor = Sensor.isSupported(Sensor.HOLSTER);
            hasSliderSensor = Sensor.isSupported(Sensor.SLIDE);
            statusField1.setText("Flip: " + hasFlipSensor + "\nHolster: "
                    + hasHolsterSensor + "\nSlider: " + hasSliderSensor + "\n");
            // Check for the state of a sensor
            if (Sensor.isSupported(Sensor.SLIDE)) 
            {
                int sliderState = Sensor.getState(Sensor.SLIDE);
                switch (sliderState) 
                {
                    case Sensor.STATE_SLIDE_CLOSED:
                        // do something if slider is closed
                        statusField2.setText("Slider is closed.");
                        break;
                    case Sensor.STATE_SLIDE_IN_TRANSITION:
                        // do something if slider in transition
                        statusField2.setText("Slider is between open and closed.");
                        break;
                    case Sensor.STATE_SLIDE_OPEN:
                        // do something if slider is open
                        statusField2.setText("Slider is open.");
                        break;
                    default:
                        statusField2.setText("Can't determine state of slider.");
                }
            }
            // Listen for changes to a sensor
            Sensor.addListener(Application.getApplication(), this,
                    Sensor.SLIDE);
        }

        // implementation of SensorListener.onSensorUpdate()
        // only checks for slider changes
        public void onSensorUpdate(int sensorID, int update) 
        {
            if (sensorID == Sensor.SLIDE) 
            {
                if (update == Sensor.STATE_SLIDE_OPEN) 
                {
                    // do something if slider is now open
                    statusField2.setText("Slider is now open.");
                } 
                else if (update == Sensor.STATE_SLIDE_IN_TRANSITION) 
                {
                    // do something if slider is now transitioning
                    statusField2.setText("Slider state is changing.");
                }
                else if (update == Sensor.STATE_SLIDE_CLOSED) 
                {
                    // do something if slider is now closed
                    statusField2.setText("Slider is now closed.");
                }
            }
        }
    }
}
Next topic: Message list

Was this information helpful? Send us your comments.