BlackBerry Development Fundamentals

What people have said about the book:

It was readable and informative. I don’t use the product or write apps, so it’s more than I can fully understand. That said, it reads well and from what you indicated it fills a niche that needs filled.

Stephen Wargo
(Yes, the author's brother)

Home Inside the Book Sample Code Chapter 11 - Sensor Test
Chapter 11 - Sensor Test PDF Print E-mail

Chapter 11 includes a sample BlackBerry Java application that demonstrates how to use the methods available in the Sensor class (net.rim.device.api.system.Sensor) to obtain information about a BlackBerry device. Here's the code:

package com.bbdevfundamentals.SensorTest;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.SensorListener;
import net.rim.device.api.system.Sensor;

//Create the HelloWorld class
public class SensorTest extends UiApplication implements SensorListener {

 public static void main(String[] args) {
 // Instantiate the HelloWorld object
 SensorTest st = new SensorTest();
 // Enter the Event Dispatcher
 st.enterEventDispatcher();
 }

 public SensorTest() {
 // Create the HelloWorldScreen and open it
 pushScreen(new SensorTestScreen());
 Sensor.addListener(Application.getApplication(), this, Sensor.HOLSTER);
 }

 public void onSensorUpdate(int sensorId, int update) {
 if (update == Sensor.STATE_IN_HOLSTER) {
 Dialog.alert("Device is holstered");                
 } else {
 Dialog.alert("Device is not holstered");
 }
 }
}

final class SensorTestScreen extends MainScreen {

 public SensorTestScreen() {
 super();
 // Create the screen's title
 LabelField lblTitle = new LabelField("Sensor Test", LabelField.ELLIPSIS
 | LabelField.USE_ALL_WIDTH);
 setTitle(lblTitle);
 }

}

I wrote a little bit about this application over at www.johnwargo.com, I had an application that used DeviceInfo.isInHolster() and all of a sudden it stopped working in the 4.7JDE. I had to learn a different way to do it and decided to add it to the book.

Attachments:
Download this file (SensorTest.zip)SensorTest.zip2 Kb26/10/09 09:40
 
InformIT (Pearson Education)