I want to create a simple Blackberry app that plays an audio file whenever I charge my phone, and for the application to close when I unplug it.
Pseudocode
Start application when battery cable plugged in,
application plays sound continuously while chargingcould not make it loop without a gap of silence in between, instead play a sound
if user unplugs cable, stop the stream, play a sound, stop the stream
optional: if battery level falls to critical/done charging, play a sound
Looking through the docs I think there isn't a listener to tell you if battery is at 100%.
Edit: Found a way through batteryStatusChange, and thanks Nate for helping me out there
Having null exception errors.
Edit: Used InputStream and no more null exception errors. Added wav files to the res folder. New code below plays a sound at 100 and two different sounds, one for USB connect and another for USB disconnect.
public class HelloBlackBerryScreen extends MainScreen implements SystemListener2 {
private BasicEditField basicEditField;
private Player HEV;
private String wav;
private InputStream stream;
private int volume; //going to set volume from GUI using a drop down list, working on it currently
public HelloBlackBerryScreen()
{
super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
setTitle( "HelloBlackBerry" );
add(new RichTextField("Battery", RichTextField.TEXT_ALIGN_HCENTER));
Application.getApplication().addSystemListener(this);
wav = "voice_on.wav";
stream = (InputStream)this.getClass().getResourceAsStream("/" + wav);
try {
HEV = Manager.createPlayer(stream, "audio/wav");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void batteryGood() {
// TODO Auto-generated method stub
}
public void batteryLow() {
// TODO Auto-generated method stub
}
public void batteryStatusChange(int status)
{
// TODO Auto-generated method stub
if ((status & DeviceInfo.BSTAT_LEVEL_CHANGED) != 0)
{
if(DeviceInfo.getBatteryLevel() == 100)
{
try
{
setWav("power_level_is_100_percent.wav");
HEV.start();
stream.close();
}
catch (MediaException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void powerOff() {
// TODO Auto-generated method stub
}
public void powerUp() {
// TODO Auto-generated method stub
}
public void backlightStateChange(boolean on) {
// TODO Auto-generated method stub
}
public void cradleMismatch(boolean mismatch) {
// TODO Auto-generated method stub
}
public void fastReset() {
// TODO Auto-generated method stub
}
public void powerOffRequested(int reason) {
// TODO Auto-generated method stub
}
public void usbConnectionStateChange(int state)
{
// TODO Auto-generated method stub
if (state == USB_STATE_CABLE_CONNECTED)
{
try
{
setWav("suitchargeok1.wav");
HEV.start();
stream.close();
}
catch (MediaException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (state == USB_STATE_CABLE_DISCONNECTED)
{
try
{
stream.close();
setWav("battery_pickup.wav");
HEV.start();
stream.close();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String getWav() {
return wav;
}
public void setWav(String wav) {
this.wav = wav;
stream = (InputStream)this.getClass().getResourceAsStream("/" + this.wav);
try {
HEV = Manager.createPlayer(stream, "audio/wav");
HEV.realize();
VolumeControl volume = (VolumeControl) HEV.getControl("VolumeControl");
volume.setLevel(25);
HEV.prefetch();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean onClose()
{
UiApplication.getUiApplication().requestBackground();
return true;
}
}