Keywords: Java; J2me; Kjava Source: http://www.j2me.com The debut of the Motorola A6288, 388 mobile phone is accompanied by a highlight: itself with Kjava virtual machine (KVM), can pass through a variety of The way downloaded and can run the user who comply with the MIDP1.0 specification with Kjava. Later, there were a variety of mobile phones to support KJAVA. In fact, KJAVA can not only write applications for mobile phones such as A6288, but also written applications for other mobile information devices. Such embedded programs are compared to a general Java program, this article will introduce KJAVA and its applications.
Introduction to Kjava
The Java language was originally designed for embedded systems. In Java 2, in Java 2, in order to distinguish a variety of applications, it is divided into Java 2 Enterprise Edition (J1E), Java 2 Standard Edition (J2SE) and Java 2 Micro Edition (J2ME) three versions, where J2ME is also known as Kjava.
In J2SE, it defines the core class function library of the Java specification (ie Java. *) And extended class function library (ie javax. *), Which is mainly positioned at the client. J2EE is primarily located on the server-side application, in addition to supporting the core class function library defined by J2SE, add some extension function class libraries, such as supporting Serverlet / JSP javax.servlet. *, Etc. J2ME only supports part of the core function class library defined by J2SE. In addition, some extended class functions that support embedded systems are added in J2ME, such as javax.microedition. *, Etc.
Also embedded equipment, they still have a big difference in calculating capacity, power supply, etc.. The Connected Limited Device Configuration (CLDC) specification is those embedded devices, limited power supply, limited power supply, etc., and the CONNECTED Device Configuration (CDC) specification describes the capacity of the TV set-top box, power Supply sufficient system. J2ME supports these two different configurations, respectively. Under the same configuration, J2ME also defines the extended class function library associated with a particular embedded device via PROFILE.
In a conventional Java environment, in order to prevent the program from being tampered with in the transfer and other safety considerations, when the program is loaded by the category load, the BYTE CODE review is followed, and the Java virtual machine is allowed later. Execute it. This operation does not have any problems on the speed on the PC, but in such systems described in CLDC, you want to do exactly the same processing, from processing capabilities and speed, it seems that some power is not from heart. In order to solve this problem, programming needs need to do more in addition to the end of the program design: pre-audit. By pre-review, some special symbols are added in the final class file, and when the program is downloaded to the target platform, the audit operation can be completed at a faster speed.
2. Motorola SDK
The following uses the Motorola A6288 mobile phone to specifically explain KJAVA programming. Two CPUs were used in Motorola A6288, a Dragon Ball Vz 33MHz for personal digital processing, and another processing for communication. The system reserved approximately 1 M memory space to support the KJAVA application that complies with the MIDP 1.0 specification. We can go to Motorola's official website to download and develop tools: CodeWarrior (trial version), the following content is based on this development tool. In CodeWarrior, it contains the SDK of Motorola, which implements CLDC and MIDP 1.0. It achieved the CLDC class library with java.io. *, Java.lang. *, Java.util. * And javax.microedition.io. *, Implemented MIDP class libraries have javax.microedition.lcdui. *, Javax. Microedition.rms and javax.micromedition.midlets.
We are embedded in the PC environment called Applet, and the program written must extend its own class from the Applet class. The kjava program written for mobile information devices such as mobile phones is called MIDlet, and the program must expand its own class from the MIDlet class. A MIDLET program has the following typical structure:
Public Class HelloWord Extends MIDLET
{
HelloWord ()
{...}
Public void startApp ()
{...}
Public void pauseApp ()
{...}
Public void destroyApp (Boolean Unconditional)
{...}
}
When the program is started, startApp () is called by the system. (The program can be started again after the program is suspended when the program is started. PauseApp () is called when the program is temporarily stopped. When the program execution ends, DESTROYAPP () is called. We can perform corresponding processing in each function according to the actual situation.
There is no AWT or SWING in the Motorola SDK because their implementation will cost too much resource, and the Motorola SDK is just in javax.microedition.lcdui to implement some of the LCD-based comparative design user interface features. Various Displable classes (such as form) are defined in the SDK. The currently displayed screen in the program must be an instance of a Displable, and the button, the text edit box, and the radio box, etc. can be placed in this instance.
3. Example
Below is a simple example of running on a A6288 mobile phone with Kjava. The specific procedures are as follows:
Import java.io. *;
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Public Class HelloWorld Extends MIDlet ImmmandListener
{
Private maincanvas mycanvas;
PRIVATE DISPLAY MYDISPLAY;
PRIVATE COMMAND C1, C2, C3;
HelloWorld ()
{
Mycanvas = new maincanvas ();
Mycanvas.addcommand (C1 = New Command ("first", command.screen, 1);
Mycanvas.addcommand (C2 = New Command ("SECOND", Command.Screen, 1));
Mycanvas.addcommand (C3 = New Command ("Third", Command.Screen, 1)); Mycanvas.SetCommandListener (this);
MyDisplay = display.getdisplay (this);
MyDisplay = display.getdisplay (this);
}
Public void startapp () throws MidletStateChangeException
{Mycanvas.Setup ("first");
mydisplay.setcurrent (Mycanvas);
}
Public void pauseapp () {}
Public void destroyApp (boolean unconditional) {}
Public void CommandAction (Command C, Displayable D)
{Mycanvas.Setup (c.getlabel ());
Class Maincanvas Extends Canvas
{String TS;
Maincanvas ()
{super ();
TS = New String ("first");
}
Public void setup (String S)
{TS = new string (s);
Public void Paint (Graphics G)
{G.SetColor (0xfffffff);
G.fillRect (0, 0, getWidth (), getHeight ());
g.setcolor (0);
g.drawstring ("current is the" ts, getWidth () / 2, 60,
Graphics.hcenter | graphics.top);
}
}
}
After the program is run, different menu items can be displayed according to the different menu items selected.