当前页面: 开发资料首页 → Netbeans 专题 → Visual Mobile Designer Custom Components Creating Wait Screens
摘要: Java ME applications often require connections to a network to work properly. Unfortunately, wireless networks are still relatively slow. A background task waiting for a network connection or for data to transfer can pause your application and leave users wondering if the frozen screen means they should stare at an unmoving screen for an indefinite period of time or just hang up and try again later (or never at all). So how do you, as a developer, spare users from this unpleasant, but all too common, experience
Java ME applications often require connections to a network to work properly. Unfortunately, wireless networks are still relatively slow. A background task waiting for a network connection or for data to transfer can pause your application and leave users wondering if the frozen screen means they should stare at an unmoving screen for an indefinite period of time or just hang up and try again later (or never at all). So how do you, as a developer, spare users from this unpleasant, but all too common, experience?
The answer is to add a "Wait Screen." Wait Screens allow users to execute a blocking background task (for example, connecting to a network), and show the progress of the task or display a message on the screen to inform the user that the application is still working as intended.
As you'll see in this tutorial, the Visual Mobile Designer (VMD), a graphical interface within the NetBeans Mobility Pack, simplifies the creation of wait screens by including a WaitScreen custom component you can design and then drag and drop into your application flow.
There are two ways to create background tasks for the Wait Screen component.
You can write a class which implements the CancellableTask
interface
or just use the SimpleCancellableTask
resource. You assign background
tasks to the Wait Screen using the setTask()
method.
The WaitScreen component automatically switches to a different Displayable
object when the background task is finished. If the task finishes successfully,
the Wait Screen switches to a display supplied by setNextDisplayable()
methods. If the task fails, the Wait Screen switches to a display supplied
by setFailureDisplayable()
methods. If there is no set failure
displayable default, the Wait Screen will switch to a display specified by
the setNextDisplayable()
method. If there is no next displayable
screen specified, the Wait Screen will switch back to the screen that was
previously displayed.
Note that this component is available for applications that support the MIDP 2.0 device profile only.
Before you begin, you need to install the following software on your computer:
If you are new to the NetBeans Mobility Pack 5.0, you should start with NetBeans Mobility Pack 5.0 Quick Start Guide before continuing.
Take the following steps to install the waitscreenexample sample application:
Now that you have seen the WaitScreen component in action, let's go back to the beginning and create this application. To create the application, you will do the following:
NOTE: Before you create this project, be sure to close or delete the sample WaitScreenExample project you ran in the steps above. The NetBeans IDE does not allow to identically-named projects to be opened at the same time.
WaitScreenExample
in the Project Name field. Change
the Project Location to a directory on your system. From now on, we will
refer to this directory as $PROJECTHOME. WaitScreenExample
project in the Project Window,
then choose File > New File (Ctrl-N). waitscreenexample
in the Package Name field. Click
Finish. waitscreenexample
package in the Project window,
then choose File > New File (Ctrl-N) . Under Categories, select MIDP.
Under File Types, select Visual MIDlet. Click Next. MyWaitScreenMidlet
into MIDlet Name and MIDP Class
Name fields. Click Finish. Selecting the Visual MIDlet in the Inspector window opens the Visual Mobile Designer (VMD). Clicking on the Flow design window at the top of the VMD window opens the Flow Designer, where you design the application flow.
Text Box
.Wait Screen
List
Alert
http://netbeans.org
Connection Failed
Please
Wait...
The Flow Designer window should look like the following graphic:
Notice that each component has at least one orange square next to it, which we will use later to connect the components to each other in the order of the application flow. Notice also that waitScreen1 has two squares on its right.
In this section, you add images that will be used by the Wait Screen and the Alert Screen.
$PROJECTHOME/src/waitscreenexample
folder.Now you're ready to create the flow of the application.
MyWaitScreenMIDlet
source code after
public void destroyapp(boolean unconditional) {
}
:private void getServerInfo() throws IOException { String url = textBox1.getString(); list1.deleteAll(); /** * Open an HttpConnection */ HttpConnection hc = (HttpConnection) Connector.open(url); /** * Gets a header field key and header field by index and * insert it into list1. */ list1.setTitle(hc.getURL()); for (int i=0;hc.getHeaderFieldKey(i)!=null; i++){ list1.insert(0,hc.getHeaderFieldKey(0)+" :"+hc.getHeaderField(0),null); } /** * Closing time ... */ hc.close(); }
getServerInfo();
Press <F6> to Run the main project.
Alternatively you could select Run > Run Main Project.
The NetBeans IDE provides API documentation—javadocs—for the WaitScreen component and SimplecancellableTask resource, as well as other components you can use in the VMD. To read the javadocs:
org.netbeans.microedition.lcdui
link. Then click the Wait
Screen link in the Class Summary table.org.netbeans.microedition.util
link. Then click on the SimpleCancellableTask
link in the Class summary table.