Monday, February 2, 2009

How to - Capture power change events


Your application can be notified when power change events take place on the BlackBerry device. This could be used to halt certain features when the battery power is low, and resume them when it is back to normal levels. Implementing the SystemListener interface (net.rim.device.api.system.SystemListener) can perform these operations. Events such as the BlackBerry powering on, powering off, and changes in the battery state can be captured. The following sample application shows how to implement the SystemListener interface:
Code:
/**
* PowerChangeEvent.java
*
*/

import java.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;

public class PowerChangeEvent
extends Application implements SystemListener
{
public static PowerChangeEvent theApp;

public static void main(String args[])
{
theApp = new PowerChangeEvent();
theApp.enterEventDispatcher();
}

public PowerChangeEvent()
{
//Register this instance with the system listener.
Application.getApplication().addSystemListener(this);
System.out.println("PowerChangeEvent: PowerChangeEvent has started!");
}

//Invoked when the user is putting the device into a power off state.
public void powerOff()
{
System.out.println("PowerChangeEvent: The BlackBerry is powering off.");
}

//Invoked when the device has left the power off state.
public void powerUp()
{
System.out.println("PowerChangeEvent: The BlackBerry is powering up.");
}

//Invoked when the internal battery voltage falls below a critical level.
public void batteryLow()
{
System.out.println("PowerChangeEvent: The battery is getting low!");
}

//Invoked when the internal battery voltage has returned to normal.
public void batteryGood()
{
System.out.println("PowerChangeEvent: The battery level is now good!");
}

//Invoked when the internal battery state has changed.
//Not used in this sample.
public void batteryStatusChange(int status)
{
}
}

No comments:

Post a Comment

Place your comments here...