Java mobile phone programming technology
Lin Tianfeng
(Wenzhou Vocational and Technical College computer system Zhejiang Wenzhou 325000)
Summary: Briefly introduces Sun's J2ME specification, and illustrates the design method and development process of MIDP program through specific examples. CLC NO: TP312JA Document Node: B Article Number: 1004-373X (2003) 09-057-041 Introduction With the development of communication technology and the popularity of mobile phones, people are constantly increasing new features, of which Make your phone realize only the features of the PC machine - download various programs and run, is an important direction for mobile phone development. To this end, people put forward a variety of solutions, where Java-based J2ME schemes are undoubtedly the most striking. Java language is due to its own platform, open, network function, object-oriented, safety, etc., once SUN is proposed, with the development of the Internet, it has become a compulsory language of software developers abroad. Especially the cross-platform advantages of his "one written, run everywhere", which is more important for mobile phone fields. Because there are currently a wide variety of mobile phones, various manufacturers have their own standards, how to make them running the same standard program a problem that must be solved. After using Java language, various mobile phones can run Java programs as long as Java virtual machines (JVM), which can be said to be the most ideal solution in current situation. J2ME (Java 2 Micro Edition) is a Java for embedded systems. He is Sun Company for a standard Java version J2SE to maximize the maximum streamlined Java application running environment, in the simplest case Only dozens of KB is particularly suitable for use in devices such as mobile phones, PDAs. Mobile phone manufacturers can download and run Java programs from the network in their own phones, which greatly expands the functionality of the phone. In view of the huge success of Java, we have reason to believe that J2ME can extend this success into the handheld device, providing favorable conditions for future mobile phones and Internet integration. The emergence of Java mobile phones also provides new opportunities to the software industry, and can provide enough software resources to join the Java mobile phone to be one of the important factors related to whether it can be promoted. This article makes a simple introduction to the J2ME architecture, and manifests the characteristics and development processes of the Java mobile phone program through a specific program example. 2 J2ME Overview J2ME is a new, very small Java application running environment, and the architecture he defined is mainly used to promote Java technology on hand-held devices. Sun will provide a suitable configuration for various hardware sizes, including virtual machines, core class libraries, and APIs. Currently, J2ME customizes two configurations: CLDC (Connected Limited Devices Configuration) and CDC (Connected Device Configuration). Among them, CLDC is a configuration provided by CPU and memory resources such as mobile phones. It is a configuration provided by devices requested by standard Java. He is the maximum reduction of Java standard, and its virtual machine and core library are more From 100kb. The CDC configuration is primarily used to have more resources. CLDC is configured to various handheld devices to run an environment that runs Java programs, but developers are still difficult to develop programs in this environment. First, because the CLDC core library provides low API, it is not suitable for building applications. Program; Secondly, the system structure of the handheld device is very different, it is difficult to ensure the portability of the application's graphical interface, network and other functions. Therefore, in order to further enhance the function of J2ME, Sun allows equipment vendors to provide specialized advanced APIs and program management methods for their devices on the basis of CLDC, that is, Profile to simplify the development of applications, so that the program has more Good portability.
At the same time, Sun also developed with more than 50 well-known mobile device manufacturers and software developers, SUN has developed Profile - MIDP (Mobileinformation Device Profile) specifically for mobile information devices such as mobile phones. Since MIDP provides a graphical interface, The unified norm of the network program, the dynamic, secure program management method, so the support of major mobile phone manufacturers such as Nokia, Motorola, Siemens, has introduced MIDP-based Java mobile phones. Similar to Applet, the MIDP program is also called MIDlet, and he must inherit a specific class - MIDlet, so the MIDlet administrator can control his operation. This class contains StartApp, PauseApp, DestoryApp, three users can overload, respectively, in the MIDlet, stop running, and quit. The MIDP program's graphical interface relative to the standard Java's AWT interface is much simpler, and he is divided into low-level API and advanced API. Low-level graphics API allows developers to control screen display and keyboard inputs more directly. He is generally used to write game programs based on abstract class Canvas. His portability is guaranteed by developers. Advanced graphics API can have good portability between different devices, but developers can be adjusted well, and he is based on abstract clarism, and there are four specific children of Alert, Form, List, Textbox. Class, these 4 subclasses represent alert box, form, list box, and text input box. In addition, advanced APIs also offer many graphics components, including Command, ChoiceGroup, Datefie1d, Gauge, ImageItem, StingItem, TextField, etc. They can be added to A1ERT and FORM. Some graphics components also provide events and event response processing similar to AWT. The MIDP program is generally compressed into a JAR file with various resource files. When publishing, there is also a description file that extends JAD, which contains information such as the name, version, JAR file of the MIDP program. The Java phone first has to access the description file, and then install the MIDP program to this phone according to the information of the description file, and then run. 3 Developing an instance MIDP program The real running environment is a Java phone, but his development is carried on the PC, so the development environment generally provides the simulator to test the program. Various mobile phone manufacturers have their own development, test environment, the example procedures described below, which is adopted in the MIDP implementation of Sun. 3.1 Program Function This example contains 2 MIDP programs, and the first program is required to enter the name and select gender, then the program outputs a greeting to the user according to gender and current time. The second program runs when the user selects the image name and then display this picture on the screen. These two MIDP programs are described by the same description file.
3.2 Program Source Code and Note 3.2.1 The first MIDP program (Samplel.java) Import Javax. Microedition. MIDLET. MIDlet; import javax. Microedition. 1cdui. IMPORT JAVA. Util. *; // Any MIDP program must inherit the MIDlet class. In order to process the button command, you have to implement the CommandListener interface public class sampler extends midlet imports commandListener {private textBox input; // declared text input box private form select; // Used to place a variety of graphics components private alert output // declaration alert box Command CFL, CF2, CT, CA; // Declaring various command buttons choicegroup sex; // declaration selection box Display Display = null; // Declaration Screen display Object public SampleL () {INPUT = New Textbox ("Please enter your name:", null, 8, textfield.any); // Create a text input box CT = New Command ("OK", Command.Screen, 1) ; // Create button CTINPUT. AddCommand (CT); // Add the button to the text input box INPUT. SetCommandListener (this); // Setting button Event Handling class select = new form ("Please select Gender:"); // Create a form string [] str = {"male", "female"}; sex = new choicegroup (null Choicegroup.ExClusive, str, null; // Create a radio box, select. Append (sex) // Add a single selection box to form CFL = New Command ("OK", command.screen, 1); // Create 2 buttons CF1 and CF2CF2 = New Command ("Rollback", Command.back, 2); SELECT. AddCommand (CF1); // add 2 buttons to the form Select. Addcommand (CF2); SELECT. SetCommandListener (this) // Setting button Event Handling class Output = new alert ("Greeting;"
, "Hello 1", NULL, NULL); // Create a warning box Output. SetTimeout (Alert.Forever); // Setting timeout is always CA = New Command ("Exit", command.exit, 1); // Create button CA OUTPUT.ADDCOMMAND (CA); // Add button CA to alert box Output.setCommandListener (THIS); // Setting button Event Handling class} public void startapp () // This method is called {if (Display == null) display = display · getDisplay (this); // Receive this MIDlet screen display object Display · setCurrent (Input); // Show text input box INPUT in the screen} public void pauseapp ()} // This method calls public void destroyApp when the MIDlet is lost, call public void destroyApp (Boolean)) PUBLIC VOID DESTROYAPP (Boolean) UNCONDITIONAL) / / This method calls {INPUT = null; output = null; select = null; select = null;} // CommandAction method is the button event processing method defined in the CommandListener interface PUBLIC VOID COMMANDACTION (Command C, Displayable D) {if (c == ct) // If you press the CT button Display.setCurrent (SELECT); // put the form select display on the screen if (c == cfl) {// If you press the CFL button Calendar RightNow = Calendar .GetInstance (); // Get the current time int Ti = RightNow.get (Calendar.Hour_Of_DAY); // Get the number of hours (24 hours) int index = sex.getSelectedIndex (); // Get the user STRING HELO = NULL in the check box; String name = input · getString (); // Gets the string // below the user entered in the text box to build different strings according to the value of Ti and IndX Heloif (Ti> = 12 && index == 1) Helo = "Good morning!" Name "Miss"; if (Ti> = 12 && index == 0 Helo = "Good morning!" Name "Mr."; if (T i <12 && index == 1) Helo = "Good afternoon!" Name "Miss"; IF (Ti <12 && index == 0 Helo = "Good afternoon!" Name "Mr."; Output. SetString (Helo); /
/ Add a string helodisplay · setCurrent (output) in the warning box; // Show the warning box Output on the screen} if (c == cf2 // If you press the CF2 button Display.setCurrent (Input); // Text Input Input Displays the screen if (c == ca) {// A0 fruit Press the CA button, call the destroyApp method to exit DESTROYAPP (TRUE); NOTINDESTROYED ();}}} 3.2.2 2nd MIDP program (sample2.java) import javax.microedition.midlet.MIDlet; import javax.microedition.1cdui *;. public class sample2 extends MIDlet implementsCommandListener {private form form; private list list '// declaration list box Command cl = null; Display Display = NULL; STRING [] DD = {"Image 1", "Image 2", "Image 3"}; public sample2 () {list = new list ("Test List, choice.exclusive, dd, null); CL = New Command ("OK", Command.screen, 1); List.addcommand (CL); list.estcommandlistener (this);} public void startapp () {Display = Display · GetDisplay (this); Display · SetCurrent 1IST);} public void pauseapp ()} public void design {list = null;} public void CommandAction (Command C, Displayable D) {image aa = null; // Declaration Image Object AAIF (c == CL) {// User Press the CL button Int i = list · getselectedIndIndex (); // Get the item number Try {// created by the user in the list box, the image file is TL.Png, T2 .png or t3.png, compress together with the class AA = image.createImage in the JAR package ("/ t" (New Integer (i L)). TOSTRING () "PNG");} catch (exception e) {} form f = new form (" Test "); // Create a form ff.append (aa); // Add image display · setCurrent (f) in the form; // Display the form on the screen}}} 3.2.3 Description File content (Sample . JAD) MIDLET-1; Hello, Samplel //
Declare the first MIDLET program, displaying name Hello, class named SampleLmidlet-2: image, sample2 // declaration 2nd MIDLET program, display name Image, class named sample2midlet-description: sample application // for MIDlet The number of bytes of the MIDlet-jar-size: 11640 // jar package is modified according to the actual situation. The position and name of the jar // jar package, the location is omitted, indicating the same location with JAD files in the same location MIDlet-name: mysamples // midlet-vendor: Sun microsystems // supplier name MIDlet-version: 1.0 // MIDlet version 3.3 development process (under Windows 2000 Platform) (1) Download MIDP2.0 (http: va.sun.com/products/midp) from Sun's website, unzipped to c: \midp2.0fcs 2) Download the Java Standard Development Tool JDKL from Sun's website. 3.1, installed in C: \jdkl. 3 (3) Built a working directory and put the above three documents (4), and then find 3 image files, convert to the PNG format to TL. PNG, T2. PNG, T3. PNG's name deposits the working directory (5) Enter the MS-DOS mode, transfer to the working directory, set the following environment variable: set midp_home = C: / XMIDP2. OFCSSET PATH = C: / JDKL. 3 ymww; c: / midp2. OFCS \BINSET CLASSPATH = C: / MIDP2. Offs \classes (6) Compile 2 Java files: javac * .java (7) Use the following command to pre-check 2 classes: preverify-d. Sample1preverify-d. Sample2 (8) Put the relevant files into 1 package: JAR-CVF Sample. Jar *. PAG (9) Run in the emulator: MIDP-C1ASSPATH. SAMPLE. Jar-Descriptor Sample. JAD can put Sample after completing the simulation. JADD and Sample. JAD is placed on a certain website and tests with the following command: midp-install http: //