Rarely do any of us want to wait very long to send and receive transmissions. Most of us want instant gratification. Unfortunately, network communications are sometimes slow and unable to meet our expectations. Although you are at the mercy of the network infrastructure for the speed of your transmission, you can improve the perception that users have of your application by making transmissions a background process. Running your application as a background process enables users to run another process while transmission occurs in the background.
You can run an application as a background process by using a thread. The below list illustrates how this is done. The list contains the JAD file for the sample program. The list is a skeleton of an application to which you’ll need to add code that handles the transmission, which is discussed throughout this chapter. You’ll also need to include the other process that runs in the foreground.
Two classes are defined in the program. The first is the MIDlet and the other is the Process class containing code for the transmission. The MIDlet class is very similar to other MIDlets discussed in this chapter in that a form is created containing Start and Exit command buttons. The Start button launches the background process and the foreground process. Take a look at the start section of the commandAction() method and you’ll notice that an instance of the Process class is created; then the start() method of the instance is called to begin the background process as a thread. You place code that starts the foreground process below the call to the start() method.
The Process class defines the start() method. The start() method creates a thread and then calls the start() method to run the thread. The run() method calls the transmit() method. The transmit() method contains all the code to send or receive a transmission.
Transmitting a Background Process
Here are the steps required to transmit a background process:
1. Declare references.
2. Obtain a reference to the instance of the Display class.
3. Create an instance of a Command class to exit the MIDlet.
4. Create an instance of a Command class to start the MIDlet.
5. Create an instance of the Form class.
6. Associate the instance of the Command class to the instance of the Form class.
7. Associate a CommandListener with the instance of the Form class.
8. Display the instance of the Form class on the screen.
9. Create a processing class that runs a thread and contains a method that defines the background process.
10. If the Start command is selected, create an instance of the processing class.
11. Call the start() method of the processing class.
12. Call the foreground process.
13. If the Exit command is selected, terminate the MIDlet.
14. Trap exceptions thrown within the commandAction() method.
15. If the MIDletStateChangeException is thrown, indicate that conditions are now safe to terminate the MIDlet by assigning a true value to the exit flag variable.
16. Terminate the MIDlet when the Exit command is entered the second time.
Code:
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class BackgroundProcessing extends MIDlet implements CommandListener {
private Display display;
private Form form;
private Command exit;
private Command start;
public BackgroundProcessing() {
display = Display.getDisplay(this);
form = new Form("Background Processing");
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.SCREEN, 2);
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == exit) {
destroyApp(false);
notifyDestroyed();
} else if (command == start) {
Process process = new Process(this);
process.start();
// Do foreground processing here
}
}
}
class Process implements Runnable {
private BackgroundProcessing MIDlet;
public Process(BackgroundProcessing MIDlet) {
this.MIDlet = MIDlet;
}
public void run() {
try {
transmit();
} catch (Exception error) {
System.err.println(error.toString());
}
}
public void start() {
Thread thread = new Thread(this);
try {
thread.start();
} catch (Exception error) {
}
}
private void transmit() throws IOException {
// Place code here to receive or send transmission.
}
}
The JAD file
MIDlet-Name: BackgroundProcessing
MIDlet-Version: 1.0
MIDlet-Vendor: MyCompany
MIDlet-Jar-URL: BackgroundProcessing.jar
MIDlet-1: BackgroundProcessing, , BackgroundProcessing
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-JAR-SIZE: 100
No comments:
Post a Comment