Getting Started with J2ME

xiaoxiao2021-03-05  25

1.1. Profile of J2ME comes from

Http://blog.9cbs.net/emag_mobile/archive/2005/02/23/298804.aspx

J2ME is a referusion of Java 2 Platform, Micro Edition. It is one of Sun Microstems's four platforms under Java's spleen, and the three are J2SE, J2EE, and Java Card. J2ME goals are consumer equipment and embedded equipment markets, including various electronic products, such as mobile phones, PDAs, pager, set-top boxes, etc. The development process of J2ME is also responsible by Java Community Process.

J2ME inherits Java excellent cross-platform compatibility, which is one of its advantages compared to other wireless development technologies. It provides a complete solution for a variety of applications on a small device.

1.2. Introduction to the overall architecture of J2ME

The J2ME architecture is as follows:

Various Profile (MIDP, Foundation Profile, Kjava, etc.) Various Configuration (CLDC / CDC) Host Operating Systems All Java Virtual Machines, Configuration, and Profile constitute a complete operation of J2ME programs Time environment. The corresponding Java virtual machine is determined by Configuration. For example, CDC Configuration determines CVM. PROFILE is a set of APIs located on Configuration to provide support for applications in developing specific areas. In addition, there are some tools for developing and deploying various applications. 1.2.1. Configuration Introduction The main basis of CONFIGURATION is the processor capability and memory capacity of the target device, and has nothing to do with the type and speed of its network connection. One of the reasons why doing this is even if the various small devices are large in terms of functionality and form, they usually use similar processors and quantity proximity memory, so it is based on this, which is reasonable for the landscape of them. of. At present, the two standards of J2ME Configuration is: CDC (Connected DEVICE Configuration) 1.2.1.1. CDC CDC target device for intermittently connected to the network, such as set-top box, car navigation system, etc. Relatively, it is more wider than the CLDC coverage, and its objectives have more powerful. The CDC contains a full-function Java virtual machine, similar to the virtual machine currently used by J2SE. The following list is listed below, the CDC device resource requirements defined by some official J2ME specification: (Please use the latest corresponding specification on the JCP website) The 32-bit processor has a 32-bit processor has 2m or more, which is connected to the network, which is usually a wireless intermittent connection. And have a limited bandwidth. The user interface is not a need for other requirements ... .1.2.1.2. CLDC CLDC Configuration is more wide, and its target equipment mainly includes mobile phones, PDAs, handheld computers, and more. Its processing power is weaker than the CDC device. The following list is listed below, the CLDC device resource requirements defined by some official J2ME specification: (Please submit to the latest corresponding specification on the JCP website) Have a limited power supply (usually using a battery) for JAVA (usually battery powered) To the network, it is usually a wireless intermittent connection and has a limited bandwidth. The user interface is not a need for other requirements ....

It should be noted that the main basis for distinguishing these two Configuration should be placed in processing power, memory, whether or not to use batteries. Not the type and speed of its network connection. And the boundaries between these two specifications are getting more blurred. 1.2.2. Virtual machine introduction As mentioned earlier, the function defined by the Configuration determines the corresponding virtual machine. So there are currently two virtual machines: KILO Virtual Machine, corresponding to CLDC CVM, corresponding to CDC1.2.2.1. KVM KVM is designed for resource-limited miniature devices. It comes from the "Spotless" project of the Sun Institute. KVM is a virtual machine that meets the JVM specification, except for the JVM specification in order to meet certain special needs. 1.2.2.2. CVM CVM is used to handle powerful consumer and embedded devices, which support all functions of the Java 1.3 virtual machine. 1.2.3. Profile introduces profile defines the type of device supported by the application to support the application development of specific domains. J2ME has now included a variety of Profile, such as MIDP, PDA Profile, Kjava, Foundation Profile, and more. The following only describes the MIDP. 1.2.3.1. MIDP MIDP, MOBILE INFORMATION DEVICE PROFILE, provides APIs used by mobile devices, such as mobile phones. It defines a class related to user interface, network, and persistent storage. Applications running under MIDP are called MIDlet. Subsequent sample programs are a MIDLET. MIDP allows new applications to download to user devices, such as downloading a small game to the phone and running. 1.3. Building a simple development environment This article only introduces the development activities of the J2ME wireless development kits provided by Sun. It is not included in combination with various IDEs, such as JBuilderx, etc. Not included in the development kits provided by other vendors, such as the NOKIA S40 Series development kit. If you are interested, you can learn from other materials. 1.3.1. Downloading JDK and J2ME Wireless Development Kit (WTK) These can be found free of charge for Sun's official website, and it is also very convenient installation. This article is based on JDK1.4.2 and WTK1.0.4. You can download a higher version of the development tool. The installation path of this article is: C: / JDK142, and C: / WTK104, you can make a reasonable change according to your situation. 1.3.2. Set the environment variable on Windows 2000 and XP, you can usually set this: Right click on My Computer -> Properties -> Advanced -> Environment Variables -> System Variable, then add JDK and WTK path, That is to add C: / JDK142 / BIN and C: / WTK104 / BIN to the back of the PATH variable. Verify that the following is set correctly, you can enter the preveriry command contained in the command line to enter the WTK. If the system prompts the command cannot be found, the description is not set, it has been set. In addition, set the following environment variables, you can easily compile and run the J2ME program on the command line. Variable J2MEWTK_HOME Set to: C: / WTK104 Variable MIDPAPI Set to:% J2MEWTK_HOME% / lib / midpapi.zip Variable J2MeclassPath Set to:% J2MEWTK_HOME% / Wtklib / kenv.zip;% j2mewtk_home% / watklib / kevm.jar % J2mewtk_home% / wtklib / limited. JAR can refer to the appeal method on Windows 2000 and XP, add corresponding variables.

1.4. A simple MIDLET example starts with great "K & R", "Hello World" has become a must-have course. Oh, we are also natural. This article lists the code and explains the following, then tells how to compile and run it, then tell some other knowledge. The code is as follows: //HelloWorld.javaImport javax.microedition.midlet. *; Import javax.microedition.lcdui. *;

public class HelloWorld extends MIDlet {// MIDlet display private Display display; // for displaying "Hello World" word private TextBox box = null; // Constructor public HelloWorld () {box = new TextBox ( "Hello World", " Hello World ", 20, 0);} // MIDlet application lifecycle management method, responsible for activating MIDlet public void startapp () {Display = Display.getDisplay (this); Display.SetCurrent (box);} // Midlet application lifecycle management method, responsible for suspending MIDlet, this example does not need this public void pauseapp ()}

// Midlet application lifecycle management method, responsible for release resource public void design {box = null;}} Analyze the code below. This class inherits the javax.microedition.midlet.mIDlet class. This class is the parent class of all MIDlets in J2ME. The Display class comes from javax.microedition.lcdui.display. Represents the display of the MIDP device. The display can be obtained through the static method of the Display class, and the HelloWorld MIDLET can be displayed on the device by static method setCurrent () so that the Hello World is displayed on the device. Textbox is a carrier of the string "Hello World", which is instantiated in the constructor. Startapp (), pauseapp () and destroyApp (Boolean Unconditional) are the application management system (Application Management System) to manage MIDlet execution, saying that they are administrative methods for application lifecycle. Developed Applet readers can phase compare with Applet's start (), STOP () and DESTROY (). There are three possible states of applications: running status, pause status, and destroy state. The conversion relationship between these states and methods is as follows: Public void startapp (): MIDlet When transitioning from the pause, you need to call this method, usually initialize all the objects required to run the MIDlet, and set the current display. It should be noted that this method can be called multiple times, so for those programs that only once, do not put them in this method, but in the constructor of the MIDlet. Public void PauseApp (): MIDLET requires calling from running steering. All activities such as suspend current running are generally performed. Public Void DestroyApp (Boolean Unconditional): When the application is no longer required, the method needs to be called when you want to enter the destruction state. It will release all resources of the application. It can be transferred from the pause state to the destroy state, or can also be transferred from the running state to the destruction state. 1.4.1. Compilation For convenience, place this program source file in C: /. First create a TMPCLASESSSS directory in this directory for use. Use the command line to enter the following command compiler: javac -d tmpclasses -bootclasspath% MIDPAPI% -ClassPath% J2meclassPath% HelloWorld.java This command compiles the appeal and creates a HelloWorld.class file in the TMPClasses directory. -bootclasspath and -classpath The role is to set the correct startup path and classpaths, so that only J2ME is used. 1.4.2. Pre-validation In the J2SE environment, you can run the Java program only in the class file, but in the J2ME environment, it is necessary to have pre-verified this because of the safety consideration, any Class files that enter the runtime. To verify the Byte Code Verifier to ensure that the program code is safe and effective. For example: not overflow the stack, does not include malicious code, after reasonable initialization, etc. Only by the code of the audit will be executed by the JVM.

This is acceptable for hardware requirements for the execution environment of ordinary applications. But for devices like CLDC, their resources are too limited. If you expect to perform the same verification program as J2se VM at runtime, it is unrealistic. Therefore, we need to handle most of the verification work to the machine to develop the MIDlet program, and the MIDlet target device only needs to perform very little verification. This has led to the emergence of pre-verification steps. During pre-verification, some special symbols are added to the Class file to simplify the validation of the target device. Before pre-verification, you can create a class directory to save the Class files passed by the verification. The specific command is as follows: preveri -classpath% MIDPAPI%; TMPClass -d classes tmpclasses1.4.3. Creating a JAR file first enters the classes directory, then execute: jar cvf helloworld.jar HelloWorld.class This will compare all classes and resource compressed Go to the JAR file and create a package list. 1.4.4. Creating a JAD file JAD (Java Application Descriptor) file for some properties of the program included in the JAR file, such as size, and so on. At the actual deployment, the file will be downloaded to the target device, processing and decide whether to download the real JAR file. The JAD file of this example is as follows, HelloWorld.jad: MIDlet-1: hello ,,, helloworldMidlet-name: helloWorldMidlet-version: 1.0midlet-verdor: oramidlet-jar-url: helloWorld.jarmidlet-jar-size: 860 This file content is very Clear, including the name, version, JAR file address, and the like. About the specific grammar of the JAD file, this article does not make specific introductions, you can refer to the resources. 1.4.5. Running in the emulator first, you need to put JAD and JAR files in the same directory, such as Classes. Then run the following command: emulator -xdescriptor: helloWorld.jad1.5. Development Skills, Performance Precautions Use local variables. Using local variables is faster than using class members. Use StringBuffer instead of a string. String series causes a lot of object creation and recycling, reducing performance. Use less as possible to call. Since the JVM uses the stack to load and store methods call, it should be called less use, and the overhead should be reduced. Try to use the object as much as possible. The creation and recycling of objects will bring overhead, so we should carefully designed, try to use existing objects. For example, the reference to the object is passed, and then modify the value of the object. Use threads to avoid synchronization. For operations for longer running time, a separate thread is required to avoid synchronization improve performance. Optimize the PNG image. Use "obscurer". In addition to preventing the Class file from being retrograd, the size of the Class file can be reduced to some extent. 1.6. Summary As mentioned in the abstract of this article, this is an article entry. To this end, the article abandoned a lot. The author believes that those content are more suitable for articles in other topics. Oh, I hope this article can bring you some gains to help you take a quick train developed on J2ME. Welcome to correct, welcome to discuss

转载请注明原文地址:https://www.9cbs.com/read-32370.html

New Post(0)