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

Sample "Please Wait" screen - part 2

by Developer on ‎07-02-2010 03:17 PM - edited on ‎09-20-2010 04:25 PM by BlackBerry Development Advisor (14,772 Views)

At the end of  Sample "Please Wait" screen - part 1 there were three enhancements to be made:

 

  1. Status updates from the Background Thread
  2. "Time to go" indication
  3. Being cancelled by the BlackBerry® smartphone user

 

In this article, we will only address one of these:

3.  Being cancelled by the BlackBerry smartphone user

 

The idea is simple — we want to add a 'Cancel' button to our PleaseWaitScreen, and have this stop the associated Background processing. To do this, we need to do three things

  1. Provide a method in our running process that can be used to stop it
  2. Define a way to tell the PleaseWaitScreen that it can use this method
  3. Add a button to the PleaseWaitScreen that can be pressed to cancel the processing

 

1) Changing our process so that it can be stopped

 

To do this, we add a new Boolean variable to our Background processing called _stop. This variable should be checked by the Background processing at key points, and if it has been set, then the processing should do just that (for example, stop).

 

Because a 'stop' could happen at any time, the background processing may have to have a number of checks for _stop being set like this:

 

if ( _stop ) { return; }

 

In most cases, the stop() method will do more than just set the _stop flag. It will also attempt to wake up the Background processing. In our case, the longest part of the Background process is the sleep. To allow the stop() method to break into this sleep, the Thread sleep processing has been changed to use wait/notify. This way of using wait/notify, where the wait will actually timeout after a period, can be very useful in other circumstances too (such as when a Background Thread is waiting for some data to process, but should only wait for limited time), so research this mechanism if you do not already know how it works.

 

We also add another method, so that external processing can check to see if a stop has been requested. All the _isStopping does is check to see if _stop has been set.

 

2) Tell the PleaseWaitScreen it can stop the processing

 

We have not told the PleaseWaitScreen anything about the processing it is running. All we have told it is that the processing is Runnable, and the PleaseWaitScreen uses the run() method that the Runnable Interface provides to execute the processing.

 

This makes the PleaseWaitScreen very general; it is not tied to one particular Object. We want to do the same for this. So, we use the same Java® concept. 

 

We create an interface, called (unsurprisingly) Stoppable. This interface has two methods, stop() and isStopping(), as shown:

 

/**
* This interface is designed to provide a common method for objects that execute code
* and wish to provide an Observer with the opportunity to stop them.
*/
public interface Stoppable {

/**
* Invoke this method to indicate to the Object that it should stop processing.
* @return true, if the Object has recognized this and will stop
* Note that an already stopped Object should return true.
*/
public boolean stop();

/**
* Indicate if this Object has already been stopped or is not active
* in other words, a stop is not required to stop the processing.
* @return true, if the object has been, or is the process of stopping.
*/
public boolean isStopped();

}

 

 

Any Object can implement this interface. In our case, our Background processing already has implemented this, so to complete this step, we need to update the source to indicate that it has implemented Stoppable, as well as Runnable. 

 

3) Adding a Cancel button (if it is supported)

 

We want to extend the PleaseWaitScreen so that it will continue to work our existing Runnable Objects (which can't be stopped) and also support Runnable processing that is Stoppable. To achieve this, the constructor checks to see if the Runnable process being executed is, in fact, Stoppable. If it is Stoppable, the processing adds a 'cancel' button, and, using a FieldChangeListener against this button, wires up a call to stop(). If the Runnable is not Stoppable, then the button is not added, so the processing is the same as it was before. 

 

In the sample, the code actually adds two buttons. A second 'hide' button is added, which will put the Application into the Background. This just allows the BlackBerry smartphone user to get on with something else while they are waiting. Of course, there are other ways for putting the Application into the background, so this is just a convenience for the user.

 

See the associated .zip archive for the full source included with this article. 

 

Of course, we still have two more enhancements to make:

a) Status updates from the Background Thread

b) "Time to go" indication

Stay tuned...

 

Additonal Links

Sample "Please Wait" screen - part 1

Sample "Please Wait" Screen - part 3


Comments
by BlackBerry Development Advisor
on ‎06-11-2010 09:52 AM

Ready for publication.

Users Online
Currently online: 4 members 2,921 guests
Please welcome our newest community members: