Launch and close the application depending on whether the battery is charging or not

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; } 

}

+4
source share
1 answer

I need to clarify a little how you want the application to work (see my comment below your question), but I'm sure you will need to implement a SystemListener (actually, SystemListener2 , which is a kind of SystemListener ) to listen for USB status changes.

Something like this to detect a connection:

 void usbConnectionStateChange(int state) { if (state == USB_STATE_CABLE_CONNECTED) { // start playing your sound } else if (state == USB_STATE_CABLE_DISCONNECTED) { // stop playing your sound, and exit, or just stay in the background } } 

Here is a link on how to add / register a system listener at device startup

See this link on the BlackBerry USB Connection Detection Forum

And API documents on SystemListener2 too

Update: as I believe the poster already understood (based on updating the code in the question), the public void batteryStatusChange(int status) method is probably a more direct callback for use here. Everything else is the same thing, but this is just another callback in SystemListener .

+2
source

Source: https://habr.com/ru/post/1434397/


All Articles