Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8545547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:48:58+00:00 2026-06-11T12:48:58+00:00

I want to create a simple Blackberry app that plays an audio file whenever

  • 0

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

  1. Start application when battery cable plugged in,

  2. application plays sound continuously while charging

    could not make it loop without a gap of silence in between, instead play a sound

  3. if user unplugs cable, stop the stream, play a sound, stop the stream

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

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:48:59+00:00Added an answer on June 11, 2026 at 12:48 pm

    I’d need a little more clarification on how you want the app to work (see my comment below your question), but I’m pretty sure you’re going to need to implement a SystemListener (actually, a SystemListener2, which is a kind of SystemListener) to listen for USB state changes.

    Something like this to detect the 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 on device startup

    See this BlackBerry forums link on detecting USB connection

    And the API docs on SystemListener2, too

    Update: as I believe the poster already figured out (based on the code update in the question), the public void batteryStatusChange(int status) method is probably the more direct callback to use here. Everything else is the same, though, as that’s just another callback in SystemListener.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a simple app that shows me the city of the
I want to create simple Spring MVC Hello world application that runs under Jetty
I want to create a simple web based application on Android that loads certain
I want to create simple application that detects language(using Google API) of phrase and
I want to create simple app able to edit images. Main view of app
JavaScript. I want to create simple script, that will be resize loaded image using
I want to create a simple app, where the user can take a photo.
I want to create a simple server application which runs on the desktop, and
I want to create a simple login functionality in WP7 app using remote MySQL
I want to create simple file viewer. What control should i use to view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.