I actually think you would be better off if you got rid of your timer entirely. I don't think a timer will efficiently give you everything you want here.
Luckily, BlackBerry has the SystemListener interface. Implement it like this:
public final class BatteryListener implements SystemListener {
/** the last battery level we were notified of */
private int _lastLevel = 0;
/** the battery percentage at which we send an event */
private int _threshold = 10;
public BatteryListener() {
Application.getApplication().addSystemListener(this);
}
public void setThreshold(int value) {
_threshold = value;
}
/** call this to stop listening for battery status */
public void stopListening() {
Application.getApplication().removeSystemListener(this);
}
private boolean levelChanged(int status) {
return (status & DeviceInfo.BSTAT_LEVEL_CHANGED) == DeviceInfo.BSTAT_LEVEL_CHANGED;
}
public void batteryStatusChange(int status) {
if (levelChanged(status)) {
int newLevel = DeviceInfo.getBatteryLevel();
if (newLevel <= _threshold && _lastLevel > _threshold) {
// we have just crossed the threshold, with battery draining
sendBatteryStatus("Battery level at " +
new Integer(newLevel) + "%!");
}
_lastLevel = newLevel;
}
}
public void batteryGood() { /** nothing to do */ }
public void batteryLow() { /** nothing to do */ }
public void powerOff() { /** nothing to do */ }
public void powerUp() { /** nothing to do */ }
}
Then, you can just create and keep one instance of this class, whenever you want your app to start monitoring the battery for you. It will send a message if the battery level drops to 10%. If the user later starts charging again, and then stops charging, and drains past 10% again, another server message will be sent.
private BatteryListener listener;
and
listener = new BatteryListener(); // start monitoring
Obviously, in the above class, you'll either have to add your sendBatteryStatus()
method to the class, or pass that class an object who implements the sendBatteryStatus()
method.
Note: I would also recommend that you not send your notification to the server on the main thread. You don't show the implementation of sendBatteryStatus()
, so maybe you already are. But if not, please use a background thread to notify your server, so the UI isn't frozen.