Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 445
Registered: ‎07-15-2008
My Device: Not Specified

Use the SystemListener in own UiApplication

I have this code to use for the SystemListener but want to use it in my own UiApplication:

 

import java.util.*; import net.rim.device.api.system.*; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; public class PowerChangeEvent extends Application implements SystemListener { public static PowerChangeEvent theApp; public static void main(String args[]) { theApp = new PowerChangeEvent(); theApp.enterEventDispatcher(); } public PowerChangeEvent() { //Register this instance with the system listener. Application.getApplication().addSystemListener(this); System.out.println("PowerChangeEvent: PowerChangeEvent has started!"); } //Invoked when the user is putting the device into a power off state. public void powerOff() { System.out.println("PowerChangeEvent: The BlackBerry is powering off."); } //Invoked when the device has left the power off state. public void powerUp() { System.out.println("PowerChangeEvent: The BlackBerry is powering up."); } //Invoked when the internal battery voltage falls below a critical level. public void batteryLow() { System.out.println("PowerChangeEvent: The battery is getting low!"); } //Invoked when the internal battery voltage has returned to normal. public void batteryGood() { System.out.println("PowerChangeEvent: The battery level is now good!"); } //Invoked when the internal battery state has changed. //Not used in this sample. public void batteryStatusChange(int status) { } }

 

 

where and how do I import this package/class that I can capture PowerOn and PowerOff events in my application?

 

 

 

If your question was answered, please accept it as a sollution and / or give Kudos ;-)
______________________________
Use the Search Field first! Maybe the question you want to ask already exists!
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Use the SystemListener in own UiApplication

I may have misunderstood the question, but it seems to me you are asking how to convert your UiApplication so that it is a SystemListener, i.e. so it gets events like powerOff().

 

There are other options, but the following is the easiest to explain:

1) add the "implements SystemListener" to your UiApplication

2) add the following in the constructor for your UiApplication

Application.getApplication().addSystemListener(this);

3) add code for the SystemListener methods to your UiApplication - you could start by just cut/pasting the code from the sample you have given.

 

That should do it....

Developer
Posts: 445
Registered: ‎07-15-2008
My Device: Not Specified

Re: Use the SystemListener in own UiApplication

Thank you, I tried that but it gives me a compilor error:

 

public class MyListnerApp extends UiApplication implements SystemListener{ ^ Note: C:\(..)\MyListenerApp.java uses or overrides a deprecated API.

 

 What did I do wrong here?

 

I used it like this:

 

public static void main(String[] args){ MyListnerApp plSample = new MyListenerApp(); plSample.enterEventDispatcher(); }

 

public MyListnerApp(){ this.addSystemListener(this); pushScreen(new MyScreen()); }

 

final class MyScreen extends MainScreen{ public MyScreen(){ super(); this.setTitle(new LabelField("MyListnerApp")); (..) } }

 

 

 

 

 

 

 

 

If your question was answered, please accept it as a sollution and / or give Kudos ;-)
______________________________
Use the Search Field first! Maybe the question you want to ask already exists!
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Use the SystemListener in own UiApplication

The way you have used it seems fine, I presume that you have coded the appropriate methods (like powerOff()) in your MyListnerApp class.

 

The message you quote is a warning message, the compilation should still go ahead.  From memory, that message suggests that you run the compilation with the deprecate flag to see exactly which line(s) in your code is causing the problem - have you done this?  I've just checked the Javadocs for SystemListener and can find nothing deprecated in there, so perhaps there is something in your code?  checked the 4.5 API - which JDE version are you using?

Developer
Posts: 445
Registered: ‎07-15-2008
My Device: Not Specified

Re: Use the SystemListener in own UiApplication

I get this compilor error as well:

 

C:\(...)MyListnerApp.java:67: MyListnerApp is not abstract and does not override abstract method batteryStatusChange(int) in net.rim.device.api.system.SystemListener

 

I use JDE 4.5.0.7

 

Thank you for helping me! 

 

If your question was answered, please accept it as a sollution and / or give Kudos ;-)
______________________________
Use the Search Field first! Maybe the question you want to ask already exists!
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Use the SystemListener in own UiApplication

Ahh, that explains your problem.

 

This is standard Java stuff.  You have told the compiler that you have implemented a SystemListener, so it will check all the methods defined by SystemListener (see Javadocs) and ensure that you have coded your own version of any abstract methods.  To make sure you have, you should take the sample code that you supplied above, cut out from that all the methods that you haven't coded in your application, and stick them in your application. 

 

I also suggest you review how Interfaces are used, this is a standard Java thing that you will find in any Java book and I am sure there are tutorials on it on the Web.  You will need to implement many other interfaces when coding BlackBerry applications.....