I want to create a simple Blackberry application that plays an audio file whenever I charge my phone, and the application closes when I turn it off.
pseudo code
Run the application when the battery cable is connected,
The app plays sound continuously while charging
couldn't make him loop without a gap between them, instead reproduce sound
if the user disconnects the cable, stops the stream, plays a sound, stops the stream
optional: if the battery level drops to a critical / done charge, play a sound
Looking through the documents, I think there is no listener to tell you if the battery is 100%.
Edit : found a way through BatteryStatusChange and thanked Nate for helping me there
Having exception exception errors.
Change Used InputStream and more errors with errors. Added wav files to res folder. The new code below reproduces sound with 100 and two different sounds, one for connecting USB and one for disconnecting USB.
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; }
}
Dog source share