1. Trang chủ
  2. » Công Nghệ Thông Tin

Core J2ME™ Technology & MIDP phần 9 ppt

56 149 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 56
Dung lượng 780,21 KB

Nội dung

435 * an image. If the parameter is 'true' the download * was successful, and the image is shown on a form. * If parameter is 'false' the download failed, and * the user is returned to the textbox. * * In either case, show an alert indicating the * the result of the download. * */ public void showImage(boolean flag) { // Download failed if (flag == false) { // Alert followed by the main textbox showAlert("Download Failure", true, tbMain); } else // Successful download { ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null); // If there is already an image, set (replace) it if (fmViewPng.size() != 0) fmViewPng.set(0, ii); else // Append the image to the empty form fmViewPng.append(ii); // Alert followed by the form holding the image showAlert("Download Successful", true, fmViewPng); } } /* * Show an alert with the parameters determining * the type (modal or not) and the displayable to * show after the alert is dismissed * */ public void showAlert(String msg, boolean modal, Displayable displayable) { // Create alert, add text, associate a sound alStatus = new Alert("Status", msg, null, AlertType.INFO); // Set the alert type if (modal) alStatus.setTimeout(Alert.FOREVER); else alStatus.setTimeout(ALERT_DISPLAY_TIME); // Show the alert, followed by the displayable display.setCurrent(alStatus, displayable); } } /* * Class – Download * * Download an image file in a separate thread * */ Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 436 class Download implements Runnable { private String url; private ViewPngThread MIDlet; private boolean downloadSuccess = false; public Download(String url, ViewPngThread MIDlet) { this.url = url; this.MIDlet = MIDlet; } /* * Download the image * */ public void run() { try { getImage(url); } catch (Exception e) { System.err.println("Msg: " + e.toString()); } } /* * Create and start the new thread * */ public void start() { Thread thread = new Thread(this); try { thread.start(); } catch (Exception e) { } } /* * Open connection and download png into a byte array. * */ private void getImage(String url) throws IOException { ContentConnection connection = (ContentConnection) Connector.open(url); InputStream iStrm = connection.openInputStream(); ByteArrayOutputStream bStrm = null; Image im = null; try { // ContentConnection includes a length method byte imageData[]; int length = (int) connection.getLength(); if (length != -1) Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 437 { imageData = new byte[length]; // Read the png into an array iStrm.read(imageData); } else // Length not available { bStrm = new ByteArrayOutputStream(); int ch; while ((ch = iStrm.read()) != -1) bStrm.write(ch); imageData = bStrm.toByteArray(); } // Create the image from the byte array im = Image.createImage(imageData, 0, imageData.length); } finally { // Clean up if (connection != null) connection.close(); if (iStrm != null) iStrm.close(); if (bStrm != null) bStrm.close(); } // Return to the caller the status of the download if (im == null) MIDlet.showImage(false); else { MIDlet.im = im; MIDlet.showImage(true); } } } Begin by looking at the code in commandAction(). Once the user selects View we display an alert and create an instance of the class that will download the image. Before leaving, we begin the download by starting the thread. else if (c == cmView) { showAlert("Downloading", false, tbMain); // Create an instance of the class that will // download the file in a separate thread Download dl = new Download(tbMain.getString(), this); // Start the thread/download Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 438 dl.start(); } The code that downloads the image is basically the same code we've used before. All that's changed is that once the download has completed, we need to determine whether or not it was successful and return this status to the MIDlet. Here is a section of code from the method getImage(). // Return to the caller the status of the download if (im == null) MIDlet.showImage(false); else { MIDlet.im = im; MIDlet.showImage(true); } There are two remaining steps. First, show an alert indicating whether or not the download was successful. Second, if the download was successful display a form with the image; otherwise, return to the textbox. These two steps are carried out over two methods: showImage() and showAlert(). The first method deciphers the result of the download and prepares a call to the second method. Notice in showImage() that if the download was successful an ImageItem is created and placed onto a form. One more important point: the final parameter passed to showAlert() is the Displayable object to show once the alert is dismissed. In the case of a successful download the form fmViewPng, containing the image, is shown. For a failed download, the textbox tbMain is displayed. public void showImage(boolean flag) { if (flag == false) { // Alert followed by the main textbox showAlert("Download Failure", true, tbMain); } else // Successful download { ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null); // If there is already an image, set (replace) it if (fmViewPng.size() != 0) fmViewPng.set(0, ii); else // Append the image to the empty form fmViewPng.append(ii); // Alert followed by the form holding the image showAlert("Download Successful", true, fmViewPng); } } Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 439 Inside showAlert() an alert component is constructed based on the parameters passed in. The last line of this method sets the current displayable to the alert, and once acknowledged by the user, the displayable becomes the value passed in from the method showImage(). public void showAlert(String msg, boolean modal, Displayable displayable) { // Create alert, add text, associate a sound alStatus = new Alert("Status", msg, null, AlertType.INFO); // Set the alert type if (modal) alStatus.setTimeout(Alert.FOREVER); else alStatus.setTimeout(ALERT_DISPLAY_TIME); // Show the alert, followed by the displayable display.setCurrent(alStatus, displayable); } Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 440 Chapter 15. MIDP FOR THE PALM OS Topics in this Chapter • Device Requirements • Download • Installation • MIDlets • Configuring Preferences o Advanced PRC Conversion o Developer (Debugging) Application o Palm OS Implementation Running MIDlets on a device with the Palm OS requires a few steps above and beyond what we've addressed up to this point. In a nutshell, the Java software must be installed and we need to convert a MIDlet(s) to the format understood by the Palm OS, a Pilot resource file (PRC). A few thoughts before moving on. All information presented in this chapter pertains to "MIDP for Palm OS, Version 1.0." If you download a newer version, there may very well be changes to both the functionality, as well as the look and feel. For the remainder of this chapter, when using the word "device" this will refer to any device running the Palm OS that has MIDP installed. If you don't have access to an actual device, see the sidebar "Palm OS Emulator." Any differences between a device and the emulator, in regard to installation or otherwise, will be noted throughout the chapter. Palm APIs There are no specific Palm APIs supported in MIDP for the Palm OS. Although this may seem to be an oversight, it is intentional. You must remember that MIDP is targeted to a wide range of mobile devices, from cellular phones to PDAs. If there was an API made available for any one device, such as the Palm, MIDlet(s) would not be portable across all devices that have a version of MIDP installed. Throughout this chapter the word synchronization will refer to the process of transferring software from a computer to a device running the Palm OS and MIDP software. It is not important whether you use the Palm Desktop =and HotSync or a third party application, as long as the end result is the same, that PRC files are successfully installed on the device. Palm OS Emulator (POSE) If you do not have a device that is running the Palm OS, but would like to write MIDlets for this platform, you can download the Palm OS Emulator (POSE): http://www.palmos.com/dev/tech/tools/emulator/ The emulator can mimic various devices, including the Palm IIIc, which supports color. Alas , the emulator does not include a ROM ima g e , which is the actual OS stored as a file. If Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 441 you join the Palm Alliance Program, you can download various ROM images onto your computer and you are off and running: http://www.palmos.com/alliance/join/ Because the emulator is running the actual OS that you'd find on a Palm device (or any device running the Palm OS), there are few differ-ences that we need to concern ourselves with. Therefore, the remainder of this chapter will only differentiate between using an emulator versus a device when necessary. Device Requirements Before going any further, you need to verify that your device is running (emulator included) at least version 3.5 or above of the Palm OS. You can locate the version number by starting the Application Launcher, followed by tapping the Menu icon, each shown in Figure 15–1 . Figure 15-1. Application Launcher and Menu Icons Once the menu appears, as shown on the left in Figure 15–2 , select Info. From the information screen choose Version. Figure 15-2. Select "Info" followed by "Version" The version number will appear across the top of the display, as in Figure 15–3 . Figure 15-3. Palm OS version number Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 442 A few more stat's: The installed footprint of the Java Software is approximately 585 kilobytes, not including heap spaces requirements and memory consumed by a running MIDlet. It is recommended that your device has at least 4 megabytes of memory. Download You can download MIDP for the Palm OS from the following location: http://java.sun.com/products/midp4palm For Version 1.0, there were two separate downloads, one for the documentation, one for the program files. I recommend you extract the entire contents of each file, which totals approximately 2 megabytes, onto a local drive off the root directory. From here forward, this will be referred to as the install directory. The files consist of the MIDP implementation for the Palm OS, demo applications in both JAR/JAD and PRC formats, utilities to convert JAR/JAD files to PRC files, documentation, and of course, the obligatory license and copyright information. Installation The Java software for the Palm OS is referred to as Java HQ (in release 1.0). This software must be installed on the device before we can run MIDlets. Installation of Java HQ is as simple as transferring the file MIDP.prc to the device. Locate this file in the install directory ( \PRCfiles) and perform a synchronization to transfer the file. Note for POSE Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 443 Right click on the emulator to bring up the options menu, see Figure 15–4. Select Install Application/Database, choose Other to locate MIDP.prc. Figure 15-4. Right click on the emulator to bring up the options menu. You can verify the installation by returning to the Applications Launcher, locating the Java HQ icon, and tapping it once. The About screen should appear as shown in Figure 15–5 . Figure 15-5. Application Launcher and Java HQ > Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 444 MIDlets This section will cover all aspects of working with MIDlets using the Palm OS. This includes converting MIDlet suites to Palm OS files, transferring MIDlets to a device, as well as running, beaming and removing MIDlets. PRC Converter Tool MIDlet's are typically distributed as a pair of files, a JAD and JAR, which comprise the "MIDlet suite." In order to run MIDlets on the Palm OS these files must be Converted to a Palm resource file (PRC). The PRC Converter Tool is a Java Swing application that is started from a command prompt, by typing "converter" in the directory \Converter, located off the install directory. Once up and running you'll see the screen shown in Figure 15–6 . Figure 15-6. Program to convert jad/jar file to .prc file JAVA_PATH Before running the converter, you must have a reference to the location of a JDK or Java Runtime Environment (JRE) directory in the environment variable JAVA_PATH. For example: JAVA_PATH=c:\jdk1.3.1 or Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... com.sun .midp. palm.database.MakeMIDPApp -v –creator abcd TodoList.jar Without specifying the –icon and -smallicon, the conversion process will create two default icons, as shown in Figure 15 9 The option –outfile was not specified, thus, the PRC file will have the same name as the JAR file, with a prc extension, TodoList.PRC Example 15.4 java -cp Converter.jar com.sun .midp. palm.database.MakeMIDPApp... "Graphics" and "Todo List." You should see a display similar to the left screen-shot in Figure 15 9 Figure 15 -9 Install MIDlets, "Todo List" and "Graphics"; Application Launcher "Icon View" on left, "List View" on right Example 15.1 Graphics.jad MIDlet-Name: Graphics MIDlet-Version: 1.0.0 MIDlet-Vendor: Core J2ME Technology MIDlet-1: Doodle, ,Doodle MIDlet-2: Translate, ,Translate MIDlet-Jar-URL: Graphics.jar... TodoList.jad MIDlet-Name: Todo List MIDlet-Version: 1.0.0 MIDlet-Vendor: Core J2ME Technology MIDlet-1: TodoList, ,TodoMIDlet MIDlet-Jar-URL: TodoList.jar 447 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com MIDlet-Jar-Size: 92 33 MIDlet-Data-Size: 1500 If you look closely at the left screen-shot in Figure 15 9 you'll see a slight difference in the icons that were created for each... JAR file: java -cp c: \midp4 palm1.0\Converter\Converter.jar com.sun .midp. palm.database.MakeMIDPApp [options] Following are the various command line options available: • -help Display the command line syntax • -icon This icon will be shown by the Application Manager when the "View by Icon" preference is chosen The icon should be 32 x 32 pixels For release 1.0 of MIDP of the Palm, only... the new directory 4 Copy Doodle.class and DoodleCanvas.class from \examples\ch9\Doodle to the new directory 5 Create a file manifest.txt with the following contents: MIDlet-Name: Graphics MIDlet-Version: 1.0.0 MIDlet-Vendor: Core J2ME Technology MIDlet-1: Doodle, ,Doodle MIDlet-2: Translate, ,Translate MicroEdition-Profile: MIDP- 1.0 MicroEdition-Configuration: CLDC-1.0 446 Simpo PDF Merge and Split Unregistered... you, see Figure 15 9 Converting a Spotlet to a MIDlet If you have a Spotless application that you would like to convert to a MIDlet, Chapter 3 of the Developer's Notes contains an abundance of information including event handling, user interface and database conversion You can find the Developer's Notes document in the following directory of your MIDP for Palm OS installation: \midp4 palm1.0-docs\dev.pdf... Command-line Converter MakeMIDPApp is a command-line PRC converter It is a Java class file stored in Converter.jar, which is the same file that contains the classes for the PRC Converter Tool Here is the command-line syntax to start the Command-line Converter: 455 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com java –cp Converter.jar com.sun .midp. palm.database.MakeMIDPApp [options]... the Chapter 9, Doodle and Translate I have combined these MIDlets into one suite The resulting PRC file is in the following directory: \examples\ch15\Graphics\Graphics.prc Here are the steps to create the PRC file: 1 Create a new directory for the MIDlet files 2 Copy bolt.png from \ examples\ch9\Translate to the new directory 3 Copy Translate.class and TranslateCanvas.class from \ examples\ch9\Translate... Joystick: In place of using the keys defined on the device, this option displays directional arrows and "Select/Fire" button in a separate window Figure 15–22 shows the MIDlet "Translate" (see Chapter 9, Example 9. 9) running with the Joystick option Figure 15-22 Game controls and the "Joystick" setting 3 Full Keypad: Setting the game control to "Full Keypad" will display a window that can generate all the... Figure 15-26 Custom icons for Todo List MIDlet 457 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Example 15.5 java -cp Converter.jar com.sun .midp. palm.database.MakeMIDPApp -creator ijkl -name Todo -longname CoreJ2ME_Todo -icon TodoLargeIcon.bmp -smallicon TodoSmallIcon.bmp -outfile TodoMIDlet.prc -v -v TodoList.jar In Figure 15–27 there are two icons for our Todo-List MIDlet . icons "Graphics" and "Todo List." You should see a display similar to the left screen-shot in Figure 15 9 . Figure 15 -9. Install MIDlets, "Todo List" and "Graphics";. Figure 15–22 shows the MIDlet "Translate" (see Chapter 9 , Example 9. 9) running with the Joystick option. Figure 15-22. Game controls and the "Joystick" setting 3. Full Keypad:. "Graphics"; Application Launcher "Icon View" on left, "List View" on right Example 15.1 Graphics.jad MIDlet-Name: Graphics MIDlet-Version: 1.0.0 MIDlet-Vendor: Core

Ngày đăng: 12/08/2014, 11:20

w