Query the accelerometer when the application is in the background

  1. Import the following classes:
    • net.rim.device.api.system.AccelerometerChannelConfig;
    • net.rim.device.api.system.AccelerometerSensor.Channel;
  2. Create a configuration for a channel to the accelerometer.
    AccelerometerChannelConfig channelConfig = new AccelerometerChannelConfig( AccelerometerChannelConfig.TYPE_RAW );
    
  3. Invoke AccelerometerChannelConfig.setBackgroundMode(Boolean), to specify support for an application that is in the background.
    channelConfig.setBackgroundMode( true );
    
  4. Invoke AccelerometerSensor.openChannel(), to open a background channel to the accelerometer.
    Channel channel = AccelerometerSensor.openChannel( Application.getApplication(), channelConfig );
    
  5. Invoke Thread.sleep() to specify the interval between queries to the accelerometer, in milliseconds.
    short[] xyz = new short[ 3 ];
      while( running ) {
           channel.getLastAccelerationData( xyz );
           Thread.sleep( 500 );
    }
  6. Invoke Channel.close() to close the channel to the accelerometer.
    channel.close();

Was this information helpful? Send us your comments.