Вы находитесь на странице: 1из 226

J2ME[rus ,book]

1.

2.

Java 2 Micro Edition ( 1) .................................................................................. 6


1.1.

Java ..................................................................................................................... 6

1.2.

Java ..................................................................... 7

1.3.

...................................................................................................... 8

1.4.

Connected Device Configuration (CDC) ............................................................ 9

1.5.

Connected, Limited Device Configuration (CLDC) ........................................... 14

1.6.

................................................................. 18

1.7.

1 ............................................................................................................... 19

MIDP ( 2)...................................................................... 20
2.1.

MIDP ............................................................................................ 20

2.2.

......................................................................................... 21

2.3.

......................................................................................................................... 22

2.4.

................................................................................................. 23

2.5.

............................................................................................................................... 25

2.6.

JAR .......................................................................................... 26

2.7.

JAR ID- ........................................................................ 28

2.8.

ecpopa ID- ................................... 29

2.9.

Pacpe u ..................................................................................................... 30

2.10.
3.

J2ME Wireless Toolkit .............................................................................. 30

MIDP ( 3) ................................................................ 32
3.1.

....................................................................... 32

3.2.

cpyypa MID- ....................................................................................... 35

3.3.

MID- ............................................................................................... 38

3.4.

MIDP ............................................. 40
1

4.

5.

3.5.

............................................................................................................ 43

3.6.

.......................................................................................................... 46

3.7.

3 ............................................................................................................... 47

( 4) ............................................................. 48
4.1.

............................................................................................................... 48

4.2.

(API) MIDP .......................................................... 49

4.3.

Opaoa oa ............................................................................................................... 50

4.4.

o .............................................................................................. 50

4.5.

............................................................................................................. 58

4.6.

........................................................................................................... 60

4.7.

..................................................................................................... 62

4.8.

4 ............................................................................................................... 64

( 5) ............................................................. 66
5.1.

MIDP .................................................................................... 66

5.2.

MIDP .......................................... 66

5.3.

............................................................................................. 68

5.4.

2 .......................................................................................................... 78

5.5.

DateField ............................................................................................................................... 80

5.6.

Stringltem.............................................................................................................................. 84

5.7.

Gauge .................................................................................................................................... 86

5.8.

Ticker .................................................................................................................................... 89

5.9.

Imageltem ............................................................................................................................. 90

5.10.
6.

..................................................................................................... 93

( 6) ................................................................ 97
6.1.

........................................................................ 97

6.2.

O ............................................................................................. 98

6.3.

.......................................................................................................... 102

6.4.

.............................................................................................................. 103
2

6.5.

..................................................................................................... 104

6.6.

.......................................................................................................... 104

6.7.

Graphics .................................................................................................................... 105

6.8.

................................................................................. 106

6.9.

.................................................................................. 118

6.10.
7.

MIDP ( 7) ............................................................... 125


7.1.

............................................................................ 125

7.2.

.............................................................. 125

7.3.

RMS ......................................................................................... 126

7.4.

................................................................................................................................ 127

7.5.

.......................................................................................................... 127

7.6.

byte [ ] .................................................................................................. 133

7.7.

Cc ................................................................................................................................ 134

7.8.

............................................................................................................... 137

7.9.

....................................................................................................... 140

7.10.
8.

............................................................................................................. 120

...................................................................................... 143

MIDP ( 8) ............................................................ 145


8.1.

MIDP ................................................................................... 145

8.2.

Cpyypa MIDP .................................................................................. 145

8.3.

...................................................................................... 147

8.4.

cpyyp ....................................................... 148

8.5.

..................................................................................................... 150

8.6.

............................................................................ 151

8.7.

................................................................... 169

8.8.

coea ............................................................................................................ 179

8.9.

J2ME J2SE ......................................................... 190

8.10.

8 ......................................................................................................... 191
3

9.

( 9) .............................................................................................. 192
9.1.

.............................................................................................................................. 192

9.2.

.......................................................................... 192

9.3.

opo..................................................................................................... 193

9.4.

Ac ........................................................................................ 194

9.5.

MIDP ....................................................................... 196

9.6.

C ..................................................................................... 197

9.7.

...................................................................................................... 198

9.8.

C ........................................................................................................ 198

9.9.

, ............................................................................ 198

9.10.

...................................................................... 199

10. ( 10) .................................................................................. 200


10.1.

10........................................................................................................ 200

10.2.

................................................................................................ 200

10.3.

................................................................................................ 204

10.4.

.............................................................................................. 204

10.5.

......................................................................................................... 205

10.6.

.................................................................................... 208

10.7.

oy .................................. 208

10.8.

.................................................................................... 208

10.9.

............................................... 209

10.10.

..................................................................................................... 209

11. ( 11) ............................................................................ 210


11.1.

, ................................................................... 210

11.2.

............................................................................... 212

11.3.

........................................................................................... 215

11.4.

..................................................................................................... 215

11.5.

.......................................................... 216
4

11.6.

............................................................................................................. 217

11.7.

........................................................................................... 217

11.8.

Apxeypa .............................................................................................. 218

11.9.

.................................................................................................. 220

11.10.

....................................................................................................... 222

1. Java 2 Micro Edition ( 1)


1.1. Java
Sun Microsystems Java,
:

Java 2 Standard Edition (J2SE);


Java 2 Enterprise Edition (J2EE);
Java 2 Micro Edition (J2ME).
J2ME
,
.
.
J2ME :

,
- , ,
( ) .

,
- ,
, ,
, .

,
, .
,
(UI). ,
.
,
. , ,

.

, - , , -
,
. - - J2ME

,
, .

J2ME ,
. J2ME
12- .

1.2.

Java

,
J2ME. ,
J2ME.

J2ME Java- .
.
,
, , Java,
Java, .
,
,
.

.

, , .

J2ME, ,
. Java,
. ,
.

. J2ME ,
,
. ,
.
, -
-
7


Java.

,
Short Message Service (SMS), .
SMS ,
, ,
.

,
. ,
, , ,
. ,
,
. .
J2ME, , . 1.1
, J2ME.

Java-

Java [JVM]

1.1. J2ME ,
Java (VM),
(API) API

,
- , .
.

1.3.
:

Java;
8

Java;
Java (API).

J2ME
. ,
, , ,
:

, -
Connected, Limited Device Configuration (CLDC,
);
- Connected
Device Configuration (CDC, ).

,
J2SE. , , , J2ME
, , .
, Java, J2SE,
J2SE.
, J2SE.
,
J2SE. ,
, J2SE ,
.

1.4. Connected Device Configuration (CDC)


Connected Device Configuration (CDC)
,
, , 2 , RAM,
ROM.

1.1,
Java, . CDC
Java 2, , ,
(Compact Virtual Machine (CVM)).

CVM. CVM , J2SE VM,


. , VM J2SE ,
.
CVM:

;
9

;
;
;
.

, CVM :

;
;
Java (ROM);
;
;

(RTOS);
Java ;
1.3, Java 2:
, , Java Native Interface (JNI, Java),
Remote Method Invocation (RMI, ), Java Virtual
Machine Debugging Interface (JVMDI, Java).

CDC. CDC API.


Java:

java.lang Java;
java.util Java;
java.net Universal Datagram Protocol (UDP) / (I/O);
java.io / Java;
Java.text (I18N
9);
Java.security
.

, API
(software development kit (SDK)) Java 2.
Java 2 SDK.
API J2SE. 1.1 ,
CDC.
1.1. CDC

CDC

java.io

java.lang

java.lang.ref


10

Java . lang. reflect

, (
)

Java .math

Java .net

Java. security

Java . security .cert

Java . text

Java . util

Java .util . jar

Java (JAR)

Java .util . zip

ZIP

javax.microedition.io

CDC

Foundation Profile.
J2ME. , ,
.
. ,
J2ME.

.
, , . J2ME
CDC, Foundation, J2SE
1.3. Java Community Process,
, . Foundation
J2SE, 1.2.

1.2. Foundation

Foundation
java.lang

Java java.lang.* J2SE (Compiler,


UnknownError)

java.util

zip J2SE (java.util.


Timer)

Java .net

TCP/IP Socket HTTP

java.io

/ Java Java , io . * J2SE


( Reader Writer)

Java .text

Java. text.* J2SE


(I18N): Annotation, Collator, Iterator
11

Java. security

, , CDC.
. , Foundation ,
, Foundation.
, Foundation CDC.
, .
, java.awt Abstract Window Toolkit (AWT,
) Java.swing Swing, API
(GUI), .
GUI, .
. J2ME, , .
GUI Foundation
, ,
, ,
J2ME, CLDC.
,
,
.
Personal Profile. Personal Java Community,
JSR-62. Personal
AWT. , ,
Web-. J2ME
Personal Java.
Personal 1.0 Foundation 1.0.
Personal Basis Profile 1.0. Personal
J2SE 1.3.1, , Personal,
J2SE 1.3.1.
1.3 , Personal 1.0.
1.3. Personal

Personal

Java. applet

, ,

Java .awt

AWT

Java . awt . data transfer

12

]ava .awt .event

AWT

Java. awt . font

Java. awt . im

Java .awt. im. spi

,
Java

Java .awt . image

Java. beans

, JavaBean

javax.microedition.xlet

,
J2ME Personal

RMI. RMI , ,
CDC. JSR-66 ,
Java Community Process.
RMI Foundation .
RMI :

RMI ;
;
RMI ;
API UnicastRemoteObject;
,
;
;
RMI .

RMI RMI API J2SE . 1.3.


RMI J2SE . 1.3 API,
RMI -
,
:

RMI ;
RMI ;
(activatable) ;
, ;
/ RMI . 1.1;
.

J2SE RMI . 1.3 :

Java. rmi. server. disableHttp;


Java.rmi.activation.port;
Java.rmi.loader.packagePrefix;
Java.rmi.registry.packagePrefix;
13

java.rmi.server.packagePrefix.

1.5. Connected, Limited Device Configuration (CLDC)


J2ME, Connected, Limited Device Configuration (CLDC),
,
, , CDC. CLDC
:

160 512 KB , Java;


16- 32- ;
, ;
( )
.

CLDC , Java .
-
CLDC , .
, ,
.
CLDC CDC .
,
. 1.2 J2SE.

1.2. CLDC CDC. CLDC, CDC, ,


J2SE, ,

CDC, CLDC Java,
Java
.
Java. CLDC
Java:
14

;
;
Java.lang.Error .

, VM 5 CLDC (Adherence
to Java Virtual Machine Specification - Java). ,
, , ,
.

Java, CLDC, VM J2SE,
. , , CLDC,
, .
float Java.lang.Float CLDC.
-
.
. , Object.finalized
CLDC.
Java.lang.Error CLDC
. , ,
.
, -,
.
. ,
, , , .
, , ,
.
.
Java . CLDC
Java. VM, -
. ,
J2SE VM, CLDC.
, CLDC- . ,
, - , -
:

Java Native Interface (JNI, Java);


;
(reflection);
(thread daemons);
( Object.finalizeQ CLDC);
(weak references);
( J2SE);
.
15


. CLDC ,
,
, J2SE . ,
, 2.
, CLDC, Kilobyte
Virtual Machine (KVM), ,
. KVM J2SE VM.
, ,
, . CLDC ,
.
, . CLDC -
, ,
Foundation CDC , CLDC

. CLDC
. CDC, OEM
( ).
1.4 , CLDC. ,
, , CDC, 1.1.
1.4. CLDC

Java. io

/ Java,
J2SE

Java . lang

VM, J2SE

Java .util

, J2SE

javax.microedition. io CLDC
Java, ,
J2SE. , ,
javax., ,
Java.
Mobile Information Device Profile. , CLDC,
,
.
Mobile Information Device (MIDP), MID Profile. MIDP
CLDC API (UI),
.
16

Java, MIDP- MID-. -


Java, MIDP CLDC.
, MID-,
J2ME CLDC/MIDP ,
J2ME. , , MIDP
, .
, PDA, . PDA
.
PDA, , , ,
MIDP, . PDA
.
MIDP, Foundation CDC,
, Mobile Information Device Profile,
,
. MIDP
(mobile information device, MID), , ,
,
:

( ) 96x54 ;
1 ;
, ;
128 MIDP-;
8 ;
32 Jra:
.

MID , MIDP
. MIDP
API:

( MIDP);
;
;
;
.

1.5. , MIDP.
1.5. MIDP
MIDP

javax.microedition. Icdui

javax.microedition.rms

(Record management system,


17

RMS],
javax.microedition.midlet

javax.microedition . io

java.io

/ Java

Java. lang

Java

Java .util

API, 1.5, 3-9.


MIDP , MIDP.
,
.
1.3 CDC CLDC. CDC, CLDC
,
. , -
-
.
ID

javax.microedition.midlet

javax.microedition . io

java.io

/ Java

Java. lang

Java

Java .util

1.3. CDC
. CLDC

1.6.
J2ME - MID- - Java,
Java VM. Java VM, ,
? ,
Java, . ,
Java
{application management software, AMS), .
AMS ,
.
AMS.
, AMS
, , ,
18

. ,
AMS .
AMS , , Java , , .
, , J2ME.
10.
, , ,
,
.

1.7. 1
J2ME .
, ,
.
, , ,
.
J2ME .
CDC Foundation ,
CLDC MIDP .
.
. ,
.
(AMS),
J2ME . AMS.

19

2. MIDP ( 2) ( bash
linux)
2.1. MIDP
, J2ME Java
Java. J2ME
Java. MIDP, ,
. ID.
, , ,
. ,
-
, . ,
,
, .
.
.
, , ,
. ,
.
,
.

, .
Java Software Sun Microsystems, , J2ME
Wireless Toolkit (J2MEWTK), Windows Unix.
, , ,
. http://java.sun.com.
J2ME
Java .
:
1. - .
2. -
J2SE Java.
3. -
Java :
Java.
4. - JAR, ,
, .
5. - .
6. - .
20

7. -
.

J2ME .
,
.
, , ,
. J2ME Wireless
Toolkit, Java Software.
, , ,
Unix, bash GNU.

Microsoft Windows MS-DOS.
, ,
, CLDC/MIDP-
. 3 ,

.
GNU Unix.
, Windows.
: , , ,
Unix , ,
PostScript, Emacs
, .
GNU Free Software Foundation (FSF).
GNU Free Software Foundation Web- Free Software Foundation,
http://www.fsf.org.

2.2.
,
, MID-. MID-
- MID-, .
MID- .
HelloWorld,
, apps/,
.
. - - ,
, , -
.

21

, :
$ pwd
/cygdrive/c/ J2rnewtk/apps/HelloWorld
3 Is -F
bin/ classes/ res/ src/ tmpclasses/
, ,
, Wireless Toolkit Emulator.
J2ME Wireless Toolkit,
. 2.1 .
2.1. , J2ME Wireless Toolkit

Bin

: .jar, .jad, MANIFEST. MF

classes

.class

Res

, , .png
PNG

Src

tmpclasses

, .class

,
. , ,
Java MIDP. , ,
MIDP-.

2.3.

. , ,
,
J2ME .
:
S javac -d <tmpclasses dir> -bootclasspath <midpapi.zip location> \
<location of Jva sourcce fie(s)>
-d ,
. -bootclasspath
midpapi.zip, J2ME Wireless Toolkit,
Java Software, MIDP,
J2ME.
22

. -bootclasspath
CLASSPATH, , , .
, (relative pathname,) . ,
Java, .
MID- HelloWorld apps/HelloWorld/,
:
$ javac -d tmpclasses \
-bootclasspach ../../lib/midpapi.zip src/HelloWorld.Java
$
-d
tmpclasses, HelloWorld/. bootclasspath . ,
HelloWorld.Java.
, MIDP CLDC
MIDP. , J2SE
CLASSPATH .
. , ,
J2SE J2ME.
tmpclasses
.class:
$ Is -I tmpclasses/
total 0
-rw-r--r-- 1 vartan None 922 HelloWorld.class
$

2.4.
.class,
. , :
$ preverify -classpath "../../lib/midpapi.zip;tmpclasses" -d classes \
tmpclasses
S
J2ME Wireless Toolkit,
, , Unix, ,
.
, . - .

23

-d ,
, . ,
, tmpclasses, ,
, .
preverify . class
:
S Is -I classes/
total 0
-rw-r--r-- 1 vartan None 922 HelloWorld.class
$
preverify ,
. CLDC, J2SE,
Java .
J2SE CLDC
:
1. 1 - ;
2. 2 - .
preverify, ,
- 1 .
, MIDP-
. ,
.
,
J2SE ,
. 50
30 100 .
CLDC RAM .
CLDC 10
100 .

, . , ,
Java.
Java. ,
. 5
, .
:

24

, ,
jsr, jsr_w, ret wide ret,
, .
,
Java.

J2SE.
J2SE.
,
Java,
Java. , CLDC
J2SE.
, CLDC,
.
StackMap_attribute. Code,
J2SE.
.
, .
Code_attribute , .
Code, J2SE.
, , Java
Java Virtual Machine Specification, .
CLDC
Stackmap_attribute, , :
StackMap_attribute
{
u2 attribute_name_index; u4 attribute_length; u2 .iumber_of_entries;
u4 byte_code_offset;
{
u2 number_of_locals;
cy types_of_locals[number_of_locals];
u2 number_of_stack_iteras;
ty types_of_stack_items[nuraber_of_stack_iterns];
} entries [number_of_entriesj;
}

, , Connected, Limited Device Configuration Specification.

2.5.
.
MID- 2 :
25

Java MID-;
.

, J2SE ,
MIDP , MID-
Java (JAR). MIDP , MID JAR. ,
MID- , MID-
JAR. , , ,
JAR-.
JAR MID- ,
:

(manifest file) - , JAR-;


Java, MID- MID- ;
, MID- MID-.

JAR (manifest file) ,


JAR-. JAR- .
, ,
MID-.
Java (JAD). MID- .
.
(AMS)
, MID- JAR
, JAR. AMS
MID-. AMS MID-.
MID- , MIDP. ,
AMS MID-, ,
MID-.
, MID- JAD- ,
MID-.
MIDP--.

2.6. JAR
Manifest MID-,
, JAR-.
. JAR- JAR J2SE.
JAR Wireless Toolkit.
MIDP , Manifest .
2.2.
26

2.2. MANIFEST.MF

MIDlet-Name

MID-

MIDlet-Versiorv

MID- <major>.<minor>.<micro>,
JDK

MIDlet-Vendor

( )

MIDlet-<n>

MID- ,
MID-, - MID-

MicroEdit ionProfile

J2ME, MID-

MicroEditionConfiguration

J2ME, MID-

, .
. ,
. MANIFEST.MF HelloWorld HelloWorld/bin/.
:
MIDlet-l: HelloWorld, HelloWorld.png, HelloWorld
MIDlet-Narae: HelloWorld
MIDlet-Vendor: Vartan Piroumian
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-1: MANIFEST.MF.
MID-, MIDlet-l MIDlet-/!. 1
MID-.
MIDlet-1 . - MID-,
MID-. , .
PNG, AMS ,
MID-. MID-,
MID-.
, MicroEdition-Configuration
MicroEdition-Profile. AMS , MID-
.
MIDP .
2.3 .
2.3. MANIFEST.MF
27

MI DietDescription

MID-

MIDlet-Icon

PNG, JAR

MIDlet-Info-URL

URL, MID

MIDlet-Data-Size

2.7. JAR ID-


, , JAR .
jar:
$ jar craf bin/MANIFEST.MF bin/HelloWorld.jar -C classes/ . -C res .
$
JAR MID- HelloWorld.
bin/ HelloWorld. jar:
$ Is -i bin
total 2
-rw-r--r-- 1 vartan None 1393 HelloWorld.jar
-rw-r--r-- 1 vartan None 193 MANIFEST.MF
$
JAR, ,
:
$ jar tf bin/HelloWorld.jar
META-INF/
META-INF/MANIFEST.MF
classes/./
classes/./HelloWorid.class
HelloWorld.png
$
, JAR. JAR
.class HelloWorld. .png (portable network
graphics - ),
. MANIFEST.MF, , ,
.

28

2.8. ecpopa ID-


, ,
, JAD ,
MID-.
, .
, . jad. ,
, MID-.
2.4.

MIDlet-Jar-URL

URL JAR MID-

MIDlet-Jar-Size

( ) JAR

MI Diet-Name

MID-

MIDlet-Vendor

(,
]

MIDlet-Version

MID- <major>. <minor>.<micro>,


JDK

MicroEditionConfiguration

J2ME, MID-

Mi croEditi on- Profile

J2ME, MID-

2.5.

MIDlet-Data-Size

MIDlet-Delete-Confirm

, AMS
MID-

MI Diet -De script ion

MID-

MIDlet-Icon

PNG, JAR

MIDlet-Info-URL

URL,
MID-

MIDlet-Install-Notify

, AMS
MID-

, 2.5, JAD
MID-,
. , ,
MIDlet- .
, MIDP.
29

JAD HelloWorld HelloWorld/bin/


:
MIDlet-1: HelloWorld, HelloWorld.png, HelloWorld
MIDlet-Jar-Size: 1393
MIDlet-Jar-URL: HelloWorld.jar
MIDlet-Name: HelloWorld
MIDlet-Vendor: Vartan Piroumian
MIDlet-Version: 1.0
, MIDlet-Jar-Size.
, JAD,
MIDlet-Jar-Size , JAR,
JAR. bin/ , JAR 1393 .
JAD , .
, , JAD.
, MIDP . ,
- MIDlet-Name, MIDlet-Version MIDlet-Vendor - .
, JAD, Manifest.
MIDP , JAR ,
.

2.9.Pacpe u
( ),
, . ,
. MID- JAR
- .
. JAR MID
.

J2ME Wireless Toolkit. ,
.
,
Wireless Toolkit , ,
, , Wireless
Toolkit. , .

2.10.

J2ME Wireless Toolkit

, J2SE Wireless Toolkit, Java


Software Sun, ,
. J2ME Wireless Toolkit Web- Java Software
30

Sun Microsystems, http://java.sun.com. ,


, , .

31

3. MIDP ( 3)
3.1.
, :
1. . ,
. J2MEWTK 1.0.2, ,
HelloWorld, ,
. 3.1 MID. , J2MEWTK 1.0.3, MID, .
2. , HelloWorld2, MID-.
, Settings... (...)
, 2.5. -,
sr /. MID-,
MIDlets (MID-) . 3.2
MID-.

32

3.1. MID-
, .
MID-

3.2. MID- MIDIets (MID) Settings ()


3. . ,
3.3. , ,
MID-, MID-. MID, , AMS ,
. , AMS
.
AMS . ,
Motorola Siemens ,
AMS, MID- , , MID-. (
, ) , Web,
AMS Web-, Java. MID33

- , AMS.
MID- , , ,
MID- . MID-,
.class JAR MID-
JAD.
J2ME, , , , -
JAR.
4. MID- HelloWorld Launch (),
. 3.4 , .

3.3. MID-, AMS ,


. AMS, , Launch ().
, -

34

3.4.
5. (hang up - )
AMS. , .
.
MID- MID-.

3.2. cpyypa MID-


, . ,
MID-. , , ,
MID- - MIDP- HelloWorld.
3.1 MID- HelloWorld.

35

3.1. MIDP- HelloWorld


import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;

/**
Hello world J2ME MIDP.
, ,
.
*/
public class HelloWorld extends MIDlet {
// Displayable. , private Form form;
// Display.
// Displayable.
private Display display;
private Form form;
// no-arg ,
// startAppO! AMS
// no-arg .
// no-arg,
//
// no-arg. public HelloWorldO
public HelloWorld() {
super();
}
public void destroyApp(boolean destroy) {
form = null;
notifyDestroyed();
}
public void pauseApp() {
}
public void startApp() {
// Displayable. form = new Form (."Hello, World");
// . String msg = "My first MIDlet!"; form.append(msg);
// , .
display = Display.getDisplay(this);
display.setCurrent(form);
}
}

36

-, , , HelloWorld,
javax.microedition.midlet.MIDlet. MID- .
HelloWorld .
(public). , (noargument) , ,
. , Java,
MID-.
HelloWorld AMS, AMS
HelloWorld. ( ,
) .
.
AMS startAppO. 3.1 startApp(), pauseApp() destroyApp()
MIDlet. ,
startApp (), . , ,
- , startApp().
startApp() MID-.
main ()? Java , Java
main () :
public static void main(String [] args)
J2ME Java,
, - ,
, .
. MIDP ( ), ,
, AMS . AMS
, , MID-
MID- . . J2ME Wireless Toolkit
Sun com.sun.midp.Main main().
startApp() , , ,
. -
javax.microedition.lcdui.Form, ,
. , -
HTML - , .
startApp() String .
, display,
.
, , 3.4.
, , AMS

37

destroyApp(), , .
. AMS MID-.
, -.
, ,
, MID- . , ,
, Java.

3.3. MID-
MID- .
MIDP . 3.1
MID- .
3.1. MID-

MID-

Paused
()

MID- . ,
.

Active ()

MID- , . ,
MID-, , MID-
.

Destroyed ()

MID- .

5 ,
MID- ,
. startApp(), pauseApp() destroyApp(), 3.1,
MID- .
MID-, MID-. MID-
, AMS.
MID-,
no-argument.
. MID- , AMS MID-
. MID-
MID- startApp ().

38

3.5. MID- . AMS


-, MID-
MID- ,
pauseApp(). MID- AMS
, notifyPausedf). MID- ,
, resumeRequest ().
AMS MID-,
, MID- destroyApp(). MID-
AMS, notifyDestroyed(). 3.2
javax.microedition.midlet.MIDlet, MID.
3.2. MID-, MID-

MID-

protected abstract void


destroyAppf)

AMS MID- . MID-


void notifyDestroyed ()

MID-

void notifyPausedf)

MID-

protected abstract void


pauseApp()

AMS MID- , MID-


void resumeRequest ()

protected abstract void


startApp()

AMS MID-,

, 3.1 System.exit().
MIDP J2SE , .
MID- MID- notifyDestroyed().
39

AMS, MID- . AMS MID-


. .
, ,
MID-. System.exit() .
MIDP.
, .
System.exit(), java.lang.SecurityException . :
java.lang.SecurityException: MIDP lifecycle does not support system exit.
( MIDP ).
at Java.lang.Runtime.exit(+9)
at Java.lang.System.exit(+7)
at HelloWorld3$MyCommandListener.commandAction(+15)
at javax.microedition.Icdui.Display$DisplayAccessor.
commandAction(+99)
at .sun.kvem.midp.Icdui.EmulEventHandler$EventLoop.run(+430)
MID- . -,
, , ,
. -, ,
. AMS. ,
, .

3.4. MIDP
MIDP javax.microedition.Icdui.
, , ,
.
MIDP .

MIDP.
3.1 .
startApp()
MIDP ,
:
display = Display.getDisplay (this);
display.setCurrentl form);
Display. Display
Java, .
: .

40

,
. MIDP
.
MID-. ,
- . MIDP
.
AWT Swing MIDP java.awt.Frame
java.awt.Window Swing AWT. MIDP
, Window
J2SE.
AMS MID-, :

Display.
Display MIDlet.

Display, MIDP . MID-


-
Displayable Item,
MID-. Display, ,
Display.setCurrent().
: MIDlet, Display,
AMS, Displayable, . 3.6
.

3.6. MIDP Display MID-. MID , MID-.


Displayable
:

Display .
41

Display Displayable.
Display, MID-
MIDP.
Displayable .

,
. 3.7
javax.microedition.lcdui.
, Display Displayable, ,
. , Form,
Hello World, Displayable, Screen.
, Form screen
.
Screen .
MIDP. Form Screen,
MID- HelloWorld.
MID- HelloWorld String Fora.
Form .
AWT Swing, MIDP . Form
MIDP, - .
: Strings, Images Items. Form
Displayable , Screen Form. ,
3.7, . ,
. MIDP
AWT Swing.
,
.
MIDP, ,
, .
, 3.7 Item Image
Displayable . Items, Images Strings
Form, 3.3.
3.3. Form

public int append (Item item)

Item

public int append (String string)

String

public int append (Image image)

Image

42

Form , String, Image Item.


,
. , Form .
, MIDP ,
AWT Swing. MIDP , Form
, .
, .
Item . , ,
, ,
. Itern.
Screen.
, Fo rm, Screen.

3.7. MIDP
MID-, Display
Displayable. , javax.microedition.lcdui.
,

3.5.
CLDC/MIDP , -,
, MIDP.
, J2SE. , CLDC/MIDP
Java.util.Properties .
MIDP ,
3.4.
, . ,
,
.
J2SE, MIDP
java.lang.System. , System
43

String getProperty(String key)


, ,
.
3.4. CLDC

mi oedit ion. con figuration

CLDO1.0

microedit ion. encoding

IS08859-1

micr oedit ion. locale

microedition. platform

micr oedition. profiles

3.2 MID-. ,
3.1.
3.2. MID-
, CLDC
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**
Hello world J2ME MIDP.
, ,
.
*/
public class HelloWorld extends MIDlet {
private Display display;
private Form form;
public void startApp() {
// Displayable. form = new Fo.rmC'Hello World");
// . String msg = "My first MIDlet!"; form.append(msg);
// , .
display = Display.getDisplay(this);
display.setCurrent(form);
printSystemProperties();
}
/**
44


System.getProperty().
*/
protected void printSystemProperties() {
String conf;
String profiles;
String platform;
String encoding;
String locale;
conf = System.getProperty("microedition.configuration");
System.out.println(conf);
profiles = System.getProperty("microedition.proflies");
System.out.println(profiles);
platform = System.getProperty("microedition.platform");
System.out.println(platform);
encoding = System.getProperty("microedition.encoding");
System.out.println(encoding);
locale = System.getProperty("microedition.locale");
System.out.println(locale);
System.out.println();
}
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
//throw new UnsupportedOperationException("Not supported yet.");
}
protected void pauseApp() {
//throw new UnsupportedOperationException("Not supported yet.");
}
}

printSystemProperties ()
startApp().
MIDP. ,
, :
CLDC-1 .0
MIDP-1.0
J2me
ISO-8859-1
enJJS
,
CLDC/MIDP.
. :
, .
45

(ISO) ,
.
, 3.2, ,
. ,
.

3.6.
MID-, JAD
MID-. , MID- . 2.4
MID-, .
MID-
.
AMS MID- , JAD- MID-
. MID- , AMS JAD-
. MID-
MIDlet
String getAppProperty(String key)
key , , MIDlet-Name.
, JAD.
3.3 , MID- . 3.2,
printAppProperties() startApp(). startApp() :
3.3. .
AMS .
public void startApp(){
// Displayable. form = new FormC'Hello, World")/'
// . String msg = "My-first MIDlet!"; form.append(msg);
// ,
// .
display = Display.getDisplay (this) ;
display.setCurrent(form);
printSystemProperties () ;
printAppProperties () ) ) ;
}
, 3.3, MIDa . 3.4 printAppProperties().
3.4. MID-, , .
MID-
,
46

/**

MIDlet.getAppProperty ().
*/
protected void printAppProperties () {
System.out.println(getAppProperty("MI Diet-Name"));
System.out.println(getAppProperty("MIDlet-Jar-Size"));
System, out. println (getAppProperty ("MI Diet-Jar-URL ")) ;
System.out.println(getAppProperty("MIDlet-Vendor"));
}
HelloWorld
, Wireless Toolkit.
printAppProperties () .
CLDC-1.0
MIDP-1.0
J2me
ISO-8859-1
en_US
HelloWorld 6781
HelloWorid.jar Vartan Piroumian
, 3.4, ,
MID-. 2 , 2.4
MID-. MIDP
, 2.5
. ,
3.3 3.4.
, MID- .
, .
MIDlet.getAppProperty(),
3.3 3.4.
MID-.
9.

3.7. 3
MIDP.
MIDP MID-.

47

MID- MID-, Display


Displayable,
.
MID- Display. MID- Displayable,
, ,
. Display
.
Screen , -
Displayable. Screen . Form
Screen. Screen MID-.
MID- .
, CLDC.
. MID. , MID-,
. ,
, MID-. JAD
AMS .

4. ( 4)
4.1.
Exit (). Command
Exit (). ! Command.EXIT
. Exit ().
, , , Command. SCREEN,
. .
,
. -,
Exit , ,
. ,


.
, . ,
, ,
.
Command, Command.
,
.

48

HelloWorldS.
, .
,
, .
, _ .
,
API MIDP. , .
,
.

4.2. (API) MIDP


,
MIDP. MID-, , ,
. ,
, ,
.
, , MIDP
.
(API) MIDP,
, .
API API
MIDP. - API, 5.
API ,
:

()
;
.

, Screen,
API.
. , , ,
. .
, Screen,
, .
API ,
. MIDP
, .

49

4.3.Opaoa oa
API MIDP .
- , -,
, , . -
. .

. , , ,
. - , , -
.
Command javax.microedition.lcdui .
:

(label);
(priority);
(command type).

- String, , ,
. int,
. -
. ,
4.1.
4.1.

public static. int BACK

public static int CANCEL

public static int EXIT

public static int HELP

public static int ITEM

,
, , ,

public static int OK

public static int SCREEN

public static int STOP

4.4. o
MIDP .
50

(command listener) , .
.
, , ,
MIDP,
. Command.
.
- , .
Displayable. To
Command Displayable
Displayable:
public void addCommand(Command crad)
public void removeCoramand(Command cmd)
Displayable

Displayable:
void setCommandListener(CommandListener cl)
Displayable. MIDP
Displayable.
MIDP. MIDP
.
.
4.1 UML Displayable Command
CommandListener.

51

4.1. UML ,
,

, UML-
, . 4.2
, .

4.2. ,

. Displayable

Swing MIDP .
API , ,
, javax.microedition.lcdui.Command-Listener.
4.1 MID- HelloWorld.

. MID- , ,
(alert), MIDP-
.
4.1. HelloWorld2
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
HellcWorld.
Displayable
-
. , Displayable
.
*/
public class HelloWorld2 extends MIDlet {
// Display. Displayable.
52

private Display display;


// Displayable. , private-Form form;
private final String ALERT_LABEL = "Alert Me!";
private Alert alert;
//
// MID-. private Command showAlert; private Command sayHi;
// ,
// CommandListener MID-.
private MyCommandListener cl = new MyCommandListener();
private Form form;
private Command showAlert;
private Command sayHi;
public HelloWorld2() {
super();
}
public void destroyApp(boolean destroy) {
form = null;
notifyDestroyed();
}
public void pauseApp() {
}
public void startApp() {
form = new Form("Hello World");

String msg = "My second MIDletl";


form.append(msg);
form.setCommandListener(cl);
showAlert = new Command(ALERT_LABEL, Command.SCREEN, 1);
form.addCommand(showAlert);
sayHi = new Command("Say Hi", Command.SCREEN, 1);
form.addCommand(sayHi);
display = Display.getDisplay(this);
display.setCurrent(form);
}
private class MyCommandListener implements CommandListener {
public void commandAction(Command c, Displayable d) {
alert = new Alert("Button pressed ",
"The" + ALERT_LABEL + "' button was pressed", null, AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, form);
}
}
53

}
ID- HelloWorld2 .

Wireless Toolkit:
? pwd
/cygdrive/c/J2mewtk/apps/HelloWorld/src
$Is
HelloWorld.lava HelioWorld2.Java
$
J2ME Wireless Toolkit .Java . HelloWorld
HelloWorld. 4.3 ,
, HelloWorld. ,
MID- . , HelloWorld2 ,
Launch ().
4.4 HelloWorld2. ,
Alert Me. ,
Alert Me!, -
.
, ,
. , .
MID- ,
J2ME Wireless Toolkit Emulator, , .
,
.

54

4.3. MID- : , AMS


, ,

4.4. MID- HelloWorld2

55

Ha 4.5 HelloWorld2,
Motorola i85s. ,
, .
Command Say Hi, .
Alert Me! ,
4.6. , HelloWorld2
, .
HelloWorld2, , ,
, ,
, startApp(). CommandListener.
HelloWorld2 , MyCommandListener,
. CommandListener.
. , , Form
CommandListener. Form,
Screen, .

4.5. - HelloWorld2
Motorola i85s

56

4.6. Alert Me! .



CommandListener
. Screen
. Java
.
MyCommandListener Form.
Command, Alert Me! .
. Alert
Me! MID-, ,
MyCommandListener, commandAction(). commandAction ()
Alert .
, .
, Command, -
, . ,
API
, . Command
- .
57

Say Hi, MID- HelloWorld2,


, , Alert
Me!, . , , Alert
, Alert Me!.
- API, ,
.
, ,

, .
Displayable,
(, ).
, ,
Command, , .
, , , -
Command. -. ,
Command, Command.
, commandAction ()
MyCommandListener, 4.2.
4.2. ,
public void commandAction(Command c, Displayable d) {
if (c == showAlert) {
alert = new Alert("Button pressed", "The '" + ALERT_LABEL
+ "' button was pressed", null, AlertType.INFO);
}
if (c == sayHi) {
alert = new Alert("Button pressed", "The "
+ "'Say Hi' button was pressed", null, AlertType.INFO);
}
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, form);
}
CommandListener Screen - .
commandAction () .
Command .

4.5.
HelloWorld2 MIDP - Screen. , ,
, . Displayable - Screen.
Alert, .

58

.

, , J2SE Swing toolkit,
, , .
,
, . ,
,
, ,
.
, .
, ,
50 32 RAM.
, .
, ,
(current displayable). , . Display
MID- Screen. , 2 , MID-
Display MIDP.
Display, ,
MID- : Display.getDisplay(midlet);
, , ,
Displayable, :
display.setCurrent(nextDisplayable)
startAppf) HelloWorld.
- :
1. .
2. , .
3. .

.
MIDP ,
.
. , , .
. , ,
.
.
, ,
, . ,
, , , .

59

4.6.
, ,
, ,
Command . -,
Command. HelloWorld2 Alert Me! Say
Hi, . , - .
Command
Displayable ,
. ,
. , Motorola
, - .
HelloWorld, HelloWorldS,
. MID- ,
, .
HelloWorld3 Command:
/**
HelloWorld.
HelloWorld2
Displayable. ,
ComraandListener ,
.
,

.
*/
public class HelloWorld3 extends MIDlet,
{
Command showAlert= new Command("Alert Me!", Command.SCREEN, 1);;
Command sayHi = new Command("Say Hi", Command. SCREEN, I);-;
Command cancel = new Command("Cancel", Command.CANCEL, 1);
public HelloWorld3()
{
super ();
}
...
}
startApp() Command :

60

form.addComraand(showAlert) ;
form.addCommand(sayHi) ;
form.addCommand(cancel);
J2ME Wireless
Toolkit Emulator ,
4.7.
-, , 4.7,

MID-
- .
.
,
.
, , .
4.8 ,
.
Motorola iS5s,
, , 4.9.
4.8 4.9 ,
.

4.7. , ,
, Displayable

61

4.8.

4.9. -

4.7.
, , , Cancel ()
, , .
62

, . ,
, Alert Me!, ,
.
,
. ,
, Command, . Command
, .
4.1.
Command HelloWorld3.
:
...
public class HelloWorid3 extends MIDlet
private Command exit = new Command("Exit", Command.EXIT, 2);
private Command help = new Command ("Help", Command.HELP, 3);
private Command item. = new Command ("Item", Command . ITEM, 4 ) ;
private Command ok = new Command("OK", Command.OK, 5);
private Command screen = new Command("Screen", Command.SCREEN, 6);
private Command stop = new Command("Stop", Command.STOP, 7);
...
}
, .
, .
startApp() .
startApp() :
public void startApp()
// Displayable. form = new Form("Hello World");
// . String msg = "My first MIDlet!"; form.append(msg);
// MyCommandListener Form,
// ,
// , form.setCommandListener(cl);
form.addCommand(showAlert); form.addCommand(sayHi);
form.addCommand(cancel) ;
form.addCommand(exit} ;
form.addCommand(help); form.addCommand(item);
form.addCommand(ok); form.addCommand(screen);
form.addCommand(stop);
// , ,
display = Display.getDisplay(this); display.setCurrentfform);
}

63

, , , , Cancel
() Exit () ,
4.10. , Cancel () ,
.

4.10. MIDP

. , ,
.

4.8. 4
(API) MIDP.
API :

;
.

MID-, API,
,
, .
64

.
, .
, ,
.
. MIDP
,
.
,
.

65

5. ( 5)
5.1. MIDP
, , API MIDP,
, ,
. ,
MIDP,
API MIDP.
.
Javadoc . ,
, ,
.

5.2. MIDP
MIDP, 5.1, ,
3.7 3.
MIDP, , Displayable, Screen, Form Alert.
, Displayable ,
, Screen
MIDP - . Screen Displayable, , a Form
.
5.1 MIDP
javax.micfoedition.lcdui.

5.1. MIDP
Displayable, Item Ticker,
Object. - , -
5.1. MIDP
66


, Ul
MIDP

Alert

AlertType

Alert

Canvas

,

/

ChoiceGroup

, Form

Command

DateField

Display

Displayable

Font

Form

Gauge

Graphics

Image

Portable
Network Graphics [PNG,
]

Imageltem

Form,

List

Screen

Stringltem

Form,

TextBox

TextField

Ticker

- API
MIDP

67

5.3.

MIDP: Displayable
Item. , 5.1, .
Displayable , . Item
, .
MIDP.
.
5.1 UIComponentDemo.java,
, MIDP.
,
.
5.1. UlComponentDemo
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**

MIDP.
.

. MID- ,
,
.
*/
public class UlComponentDemo extends MIDlet
implements CommandListener {
private Command exit = new Command("Exit", Command.EXIT, 1);
// :
// .
// ,
private static String[] demos = {"AlertDemo", "DateFieldDemo", "GaugeDemo", "StringltemDemo",
"TickerDemo", "ImageltemDemo"};
private static UlComponentDemo instance = null;
// List, ,
// demos .
private List mainMenu = new List("Select demo", Choice.IMPLICIT,
demos, null);
// No-arg.
public UlComponentDemo() {
68

super();
// super().
// no-arg MID-. super () ;
instance = this;
}
/**
.
.
^ .
*/
public static UlComponentDemo getlnstance() {
return instance;
}
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
//throw new UnsupportedOperationException("Not supported yet.");
}
protected void pauseApp() {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void startApp() {
Display display;
mainMenu.addCommand(exit);
mainMenu.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(mainMenu);
}
public void display() {
Display.getDisplay(this).setCurrent(mainMenu);
}
void quit() throws MIDletStateChangeException {
destroyApp(true);
notifyDestroyed();
}
public void commandAction(Command c, Displayable d) {
Displayable displayable = null;
if (c == List.SELECT_COMMAND) {
int index = mainMenu.getSelectedIndex();
try {
displayable = (Displayable) Class.forName(demos[index]).newInstance();
if (displayable == null) {
69

return;
}
Display display = Display.getDisplay(this);
display.setCurrent(displayable);
} catch (Exception e) {
System.out.println("Got exception here!!!");
e.printStackTrace();
return;
}
} else if (c == exit) {
try {
quit();
} catch (MIDletStateChangeException ex) {
ex.printStackTrace();
}
}
}
}

, 5.1, ,
.
MID-.
5.1 MID-. List,
, ,
. 5.2
, . List.
, . ,
, . , ,
, , , .
List.
List Screen, , , Displayable,
. 5.1, List
, ,
. MID- ,
CommandListener . CommandListener,
commandAction () .

, . ,
, ,
List. List
.
5.2
. , ,
70

.
. ,
, .
ClassNotFoundException.
J2ME Wireless Toolkit,
UIComponents/src/. . Wireless Toolkit
sic/. ,
, .class classes/.
, MID-.
, AlertDemo,
. ,
AlertDemo , 5.2.
.
5.3 , , AlertDemo
.
- - MIDP,
ChoiceGroup. , , AlertDemo.java,
5.2.
Alert.
, 5.1, , ChoiceGroup
Screen, Displayable. Item. 3, Item ,
Form. , AlertDemo
Form, TextField ChoiceGroup.
5.3 Form - AlertDemo - ChoiceGroup
TextField. , Form MIDP,
. , AlertDemo Form
ChoiceGroup TextField.

71

5.2. UIComponentDemo.

5.3. ,
TextField ChoiceGroup
72

5.2. , Command.
Displayable, ,
import javax.microedition.lcdui.*;
/**
Alert.
*/
public class AlertDemo extends Form implements CommandListener {
private Command go = new Command("Go", Command.SCREEN, 1);
private Command back = new Command("Back", Command.BACK, 1);
private ChoiceGroup type;
private TextField tPref;
private String[] elements = {"Alarm", "Confirmation", "Error", "Information", "Warning"};
//
// ,
private static Displayable instance;
/**
.
*/
public AlertDemo() {
super("Build alert");
type = buildAlertTypeSelection();
tPref = buildAlertTypePref();
append(type);
append(tPref);
addCommand(go);
addCommand(back);
setCommandListener(this);
instance = this;
}
/**
.
.
@ .
*/
static Displayable getlnstance() {
return instance;
}
private ChoiceGroup buildAlertTypeSelection() {
// He , Choice.IMPLICIT. Choice.
// IMPLICIT List,
return new ChoiceGroup("Alert Type", Choice.EXCLUSIVE, elements, null);
}
private TextField buildAlertTypePref() {
73

String MAX_TIMEOUT_VALUE = "5";


int MAX_SIZE = 8;
return new TextField("Timeout (sec.)", MAX_TIMEOUT_VALUE, MAX_SIZE, TextField.NUMERIC);
}
public void commandAction(Command c, Displayable d) {
UlComponentDemo demo = UlComponentDemo.getlnstance();
Display display = Display.getDisplay(demo);
int timeSec;
int timeMillis;
if (c == go) {
// .
String title = elements[type.getSelectedIndex()];
Alert alert = new Alert(title);
alert.setString("A '" + title + "' alert");
timeSec = Integer.parseInt(tPref.getString());
timeMillis = timeSec * 1000;
if (timeMillis <= 0) {
timeMillis = Alert.FOREVER;
}
alert.setTimeout(timeMillis);
display.setCurrent(alert, AlertDemo.getlnstance());
}
if (c == back) {
UlComponentDemo.getlnstance().display();
}
}
}
, ,
List , List,
. Build Alert ( )
ChoiceGroup -
.
, . List
Select, Build Alert (
). Build Alert ( ),
Go, Alert.
Choice - Command .
, 5.2 5.3, ,
. List ChoiceGroup
javax.microedition.ldcui.Choice, ,
. Choice
:

IMPLICIT (): ,
.
74

EXCLUSIVE (): - .
MULTIPLE (): .

List IMPLICIT.
Select List, List
, . 5.1 . ChoiceGroup
. ChoiceGroup IllegalArgumentException,
Choice.IMPLICIT.
, List.
, Displayable
Select . , , ,
Command.
List Command, List.SELECT_COMMAND. IMPLICIT
-
, .
, commandAction()
. 5.3 ,
UIComponentDemo.commandAction() .
5.3.
List.SELECT_COMMAND,
public .class UIComponentDemo extends MIDlet .
implements CommandListener
{
public void cornrnandAction (Command c, Displayable d)
{
Displayable displayable = null;
if (c == List.SELECT_COMMAND)
}
int index = mainMenu.getSelectedlndex ();
try i displayable = (Displayable)
Class.forName(demos[index]).new Instance));
Display display = Display.getDisplay(this);
display.setCurrent(displayable);
}
,catch (Exception e)
}
e.printStackTrace(); return;
}
}
else
{
return;
75

}
}
EXCLUSIVE MULTIPLE . MIDP ,

. ,
AWT Swing, ,
.
, AWT Swing.
Alert 3, . commandAction()
AlertDemo ,
Build Alert ( ), 5.3.
Alert AlertType, ,
. AlertType ,
, 5.2.
5.2. AlertType,
Alert
AlertType

ALARM ()

CONFIRMATION (]

ERROR ()

INFO ()

WARNING ()

.
,
.
-.
.
Command HelloWorld. Command
.
,
Command Alert.
, , ,
, , . ,
5 Build Alert (

76

). , 5 .
0.
0 IllegalArgumentException.
Alert.setTimeout(). Alert.FOREVER,
Done () .
, Done ().
( Web
http://www.phptr.com), Build Alert ( )
, 5.
TextField, ,
. 0, FOREVER
() .
TextField ,
. TextField Item, 5.1. TextField . TextBox,
. ,
,
.
TextField , ,
5.3.
5.3. , TextField

ANY

EMAILADDR

e-mail

NUMERIC

PASSWORD

PHONENUMBER

URL

LJRL

,
. ,
, 5.3, AND .
, CONSTRAINT_MASK
TextField.

77

5.4. 2

:

MIDlet;
Display;
Displayable;
Form;
List;
Alert;
ChoiceGroup;
TextField.

MIDP - . ,
5.3,
: .
, , , ,
.
.
MIDP . Displayable
-
.
. ,
Displayable . .
, ,
.
, ,
, , .
UIComponentDemo.java, , :
projected static Displayable instance;
:
public static Displayable getlnstance()
{
return instance;
}
static,
, -
.

78

AlertDemo Back () Build Alert


( ), 5.3. ,
. commandActionO ,
5.4.
5.4.
,

public void commandAction(Command c, Displayable d) {


UlComponentDemo demo = UlComponentDemo.getlnstance();
Display display = Display.getDisplay(demo);
int timeSec;
int timeMillis;
if (c == go) {
// .
String title = elements[type.getSelectedIndex()];
Alert alert = new Alert(title);
alert.setString("A '" + title + "' alert");
timeSec = Integer.parseInt(tPref.getString());
timeMillis = timeSec * 1000;
if (timeMillis <= 0) {
timeMillis = Alert.FOREVER;
}
alert.setTimeout(timeMillis);
display.setCurrent(alert, AlertDemo.getlnstance());
}
if (c == back) {
UlComponentDemo.getlnstance().display();
}
}
Back (), ,
List, UIComponentDemo.java, Display.setCurrent().
UIComponentDemo.getlnstance() static, List
.
AlertDemo.getlnstance () ,
, .
this. getlnstance() ,
. ,
, .
commandAction () :
UIComponentDemo demo = UIComponentDemo.get Instance ();
Display display = Display.getDisplay(demo);

79

MID-.
UIComponentDemo ,
,
:
Display.getDisplay(UIComponentDemo.getMIDiet() ) ;
, .
Displayable.
Displayable , .
, Displayable.
, , ,
.
.
.
.

5.5.DateField
UlComponent ( http://www.phptr.com/)
DateField. 5.1
, DateField Item; , Form
, . 5.5
DateFieldDemo.java.
5.5. , getlnstanceO
. Form
import java.util.*;
import javax.microedition.lcdui.*;
/**
* DateField MIDP.
* @author viktor
*/
public class DateFieldDemo extends Form implements CommandListener {
private Command back = new Command("Back", Command.BACK, 1);
private static Displayable instance;
private DateField date = new DateField("Date/Time in GMT", DateField.DATE_TIME,
TimeZone.getDefault());
public DateFieldDemo() {
super("DateField Demo");
Calendar cal = Calendar.getInstance();
date.setDate(cal.getTime());
append(date);
addCommand(back);
80

setCommandListener(this);
instance = this;
}
/**
.
..
@ .
*/
public static Displayable getlnstance() {
return instance;
}
public void commandAction(Command c, Displayable d) {
if (c == back) {
UlComponentDemo.getlnstance().display();
}
}
}

, , DateFieldDemo Form.
DateField .
DateFieldDemo ,
.
DateField , .
5.4 /, DateFieldDemo.
5.4 Date/Time in GMT (/ GMT)
. , . no-arg DateFieldDemo 5.5 ,
DateField Java.util .Calendar.
,
. DateField (
5.4), , .
5.4. DateField
DateField

public static int DATE

public static int DATE TIME

public static int TIME


81

DateField 5.5 ,
Java.util.TiraeZone. , MIDP
. ,
. , MIDP
.
DateField ,
MIDP. , ,
MIDP,
, DateField
- , , , . ,
, , DateField,
, .
DateField . ,
1. -, , 5.4.
2. Select . ,
, 5.5.
3. , , ,
.
, Back () Save
() .
. ,
, , .
DateFieldDemo, 5.4,
, , . , 5.6.

82

5.4. DateField : ,

83

5.5. DateField ,

5.6. ,

5.6.Stringltem
Stringltem . Stringltem
- . 5.7 ,
StringltemDemo, ,
.

84

5.7. :

5.6 StringltemDemo.
,
. .

5.6.
import java.util.*;
import javax.microedition.lcdui.*;
/**
*
* @author viktor
*/
public class StringltemDemo extends Form implements CommandListener {
85

private Command back = new Command("Back", Command.BACK, 1);


private static Displayable instance;
private StringItem si = new StringItem("Stringltem's title", "Immutable item text");
public StringltemDemo() {
super("Stringltem Demo");
append(si);
addCommand(back);
setCommandListener(this);
instance = this;
}
public void commandAction(Command c, Displayable d) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}
Stringltem .
String Form Stringltem, Stringltem
, , ,
.

5.7.Gauge
Gauge Item. GaugeDemo
, 5.8.

86

5.8. .

, 5.8, Form : (gauge)
String . ,
Gauge: (interactive) (noninteractive).
-,
.
.
, . ,
, . , ,
, . ,
, .
, ,
.
, .
, , ,
,
.
, . ,
5.8, ,
87

. .
, , ,
, , .

. .
, 5.7, ,
Gauge .
5.7. , ,
, ,
import javax.microedition.lcdui.*;
/**
*
* @author viktor
*/
public class GaugeDemo extends Form implements CommandListener {
private Command back = new Command("Back", Command.BACK, 1);
private static Displayable instance;
private String gaugelLabel = new String("Interactive gauge");
private Gauge interactiveGauge = new Gauge("Interactive", true, 50, 15);
private String gauge2Label = new String("Non-interactive");
private Gauge staticGauge = new Gauge("Static", false, 50, 25);
/**
.
*/
public GaugeDemo() {
super("Gauge Demo");
append(gaugelLabel);
append(interactiveGauge);
append(gauge2Label);
append(staticGauge);
addCommand(back);
setCommandListener(this);
instance = this;
}
public void commandAction(Command c, Displayable d) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}
, , ,
,
Gauge:

88

public void setValue(int value) public int getValuel)

5.8.Ticker
(Ticker) , .
TickerDemo 5.8 , 5.9.

5.9. , .
- ,

Ticker , . Ticker
Screen.setTicker (Ticker t), 5.8.
5.8. Ticker
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.raicroedition.lcdui.Ticker;
import javax.raicroedition.lcdui.Form;
/**

89

Ticker MIDP.
@see javax.microedition.lcdui.Gauge
*/
public class TickerDerno extends Form
implements CommandListener
}
private String str = "This text keeps scrolling until the demo stops...";
private Ticker ticker = new Ticker(str);
private Command back = new Command("Back", Command.BACK, 1);
private static Displayable instance;
/**
.
*/
public TickerDemo()
{
super("Ticker demo");
instance = this;
addCommand(back); setTicker(ticker) ; setCommandListener(this);
{
...
}
Ticker .
Ticker , .
5.1 , , Ticker Item.
Java.lang.Object, , Ticker
, . Item,
-, Form.

5.9.Imageltem
MIDP
. 5.10 , . 5.9
, 5.10.

90

5.10. MIDP
. Image Item,

5.9.
. ,

J2ME Wireless Toolkit
import javax.microedition.lcdui.Command;
import javax.microedition.Icdui.ComraandListener;
import javax.microedition.Icdui.Displayable;
import javax.microedition.Icdui.Form;
import javax.microedition.Icdui.Image;
import javax.microedition.Icdui.Imageltem;
import Java.io.lOException;
/**

Imageltem MIDP.
javax.microedition.Icdui.Imageltem
*/
public class ImageltemDemo extends Form implements CommandListener
{
private Imageltem imageltem;
/**
.
@ lOException, .
public ImageltemDemo() throws lOException
91

*/
super("Imageltem Demo");
String path = "/bottle80x80.png";
Image image = Image.createlmage(path);
imageltem = new Imageltem)"Ship in a bottle", image,
Imageltem.LAYOUT_CENTER,
"Image not found"); append(imageltem);
addCommand(back);
setCommandListener(this) ;
instance = this;
}
...
}
5.9 Imageltem
MIDP. Imageltem Item, Form,
.
, .
javax.microedition.lcdui.Image . Image,
. Portable
Network Graphics (PNG). J2ME .
, 5.9 res/
UlComponents. res/ ,
. - ,
lOException, .
5.9 Imageltem Image.
, ,
, ,
, - .
Imageltem ,
, MIDP
. 5.5
MIDP, .
5.5. MIDP,


MIDP

Alert

ChoiceGroup

92

List

Imageltem

ChoiceGroup List
. API ,
.
MIDP,
.

5.10.

MIDP : TextBox. TextField TextBox


.
, 5.1, , TextBox
Screen, Item.
TextBox Displayable, MID-
, Screen Form,
, Item. 5.11 TextBoxDemo.

5.11. TextBoxDemo

93

Ha 5.11 TextBox, Screen. 5.10


TextBoxDemo. , ,
UIComponentDemo -.
5.10. ,

import jav,ax.micro etiition.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.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
/**
MID-
TextBox MIDP.
javax.microedition.Icdui.TextBox
*/
public class TextBoxDemo extends MIDlet implements CommandListener
private Command quit = new Command("Exit", Command.EXIT, 1);
private static TextBoxDemo instance;
// TextBox. private TextBox textBox;
// , TextBox
// . private int MAX_SIZE = 100;
// TextBox. private String initialText =
"You can edit the contents of this TextBox";
/**
.
*/
public TextBoxDemo()
super () ; instance = this;
}
public void pauseApp()
{
...
}
public void destroyApp(boolean destroy)
}
textBox = null; initialText = null; instance = null;
}
void quit()
}
destroyApp (true);
notifyDestroyedf);
94

public void startApp()


{
texcBox = new TextBoxC'A TextBox", initialText, MAX_SIZE,
TextField.ANY); extBox.addCommand(quit); textBox.setCommandListener(this);
display();
}
/**
.
.
@ .
*/
public static TextBoxDemo getlnstance()
return instance;
}
public void display!)
{
Display. getDisplay(this).setCurrent(textBox);
}
public void commandAction(Command c, Displayable d)
if (c == quit)
{
quit();
}
}
}
, TextBox TextField, ,
. ,
, , , .
, TextField.
5.11 ,
TextBox. , TextBox
Select .
, Clear
() ,
. ,
API, , , ,
. 5.12
.

95

5.12. , ,

96

6. ( 6)
6.1.
,
MIDP,
MIDP. API MIDP, , 4.
API , API
:

(,
), ;
.
API:
javax.microedition.lcdui. Canvas;
javax.microedition.lcdui.Graphics.

6.1 MIDP,
5.1. , Canvas Displayable.

6.1. Canvas ,
, - ,
MIDP
, Canvas Screen, ,
Screen, , .
Canvas . ,
.
Graphics .
,
, Screen.
97

6.2.O
Canvas
Canvas, . Canvas
CommandListener
.
, , Canvas
. Canvas
,
.
. Canvas
API,
.
MIDP Canvas,
Canvas. 6.1 .
6.1. API

protected void keyPressedfint KeyCode)

protected void keyReleased.(int KeyCode)

protected void keyRepeated(int KeyCode)

protected void pointerPressed (int x, int y)

protected void pointerDragged (int x, int y)

protected void pointerReleased(int x, int y)

protected abstract void paint (Graphics g)

Canvas

Canvas
, 6.1.
Canvas, .
, Canvas paint (),
Canvas.
6.1 6.2 Canvas.
6.1 MID- ,
. 6.2, , Canvas - Displayable,
, 6.1, .
6.1. CanvasDemol MID-,
ID
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
98

/"
MID-, Canvas .
Canvas Canvasl.
@ Canvasl
*/
public class CanvasDemol extends MIDlet
{
// .
private static CanvasDemol midlet;
// Canvas,
// .
private static Canvasl instance;
private Display display; private Canvasl canvas;
/**
No-arg. no-arg MID-.
*/
public CanvasDemol()
super();
display = Display.getDisplay(this); instance = canvas; midlet = this;
{
/**
MID-, .
@ MID-, .
**/
public static CanvasDemol getMIDlet()
{
return midlet;
{
public void startApp()
{
canvas = new Canvasl ();
display.setCurrent(canvas);
(
public void pauseApp()
{
}
public void destroyApp(boolean destroy)
{
instance = null;
canvas = null;
void quit ()
{
destroyApp(true);
notifyDestroyed();
}
}
99

6.2. Canvas, Canvas


import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
/**
Canvas, MID-
CanvasDemol. Canvas Exit,
.
@ CanvasDemol
*/
public class Canvasl extends Canvas
implements CommandListener
{
private Command exit = new Command("Exit", Command.EXIT, 1);
/**
No-arg.
*/
public Canvasl ()
{
// super (),
// r.o-arg Canvas! . superf)
// Canvas Canvas.
// ,
// , super () ;
addCommand(exit); setCommandListener (this);
printCanvasInfo() ;
}
/**
Canvas, .
.
Canvas .
*/
public void paint(Graphics g)
{
}
public void commandAction(Command c, Displayable d)
{
if (c == exit)
CanvasDemol.getMIDlet().quit();
}
/**
,
, Canvas.
100

Canvas.
*/
public void keyReleased(int keyCode)
{
printKeyEventlnfo(keyCode);
}
/**
, .

, , Canvas.
*/
protected void printKeyEventlnf(int keyCode)
{
System.out.println("Key code = " + keyCode);
System.out.println("Key name = " + getKeyName(keyCode));
System.out.println("Game action = " + getGameAction(keyCode));
}
/*'*

Canvas.
"/
protected void printCanvasInf ()
{
System.out.println("Device height = " + getHeight ());
System.out.println("Device width = " + getWidth());
System.out.println("Pointer events = " + hasPointerEvents());
System, out. printl'n ("Pointer motion events = " +
hasPointerMotionEvents());
System.cue.println("Repeat events = " + hasRepeatEvents());
}
}
, Canvas ,
MID-, 6.2. , ,
6.2, Exit (), ID-.

101

6.2. Canvas .
,

6.3.
Canvas 1 keyReleased() Canvas.
, ,
.

: .
.
, , , .
String,
, ( ) ,
. ,
. ITU-T, 0 9, * #,
.

102

Canvas
, .
. Canvas
, 6.2.
6.2. Canvas, ITU-T
Canvas

public static final int KEY NUMO

public static final int KEY NUM1

public static final int KEY NUM2

public static final int KEY_NUM3

public static final int KEY NUM4

public static final int KEY NUM5

public static final int KEY_NUM6

public static final int KEY NUM7

public static final int KEY_NUM8

public static final int KEY NUM9

public static final int KEY POUND

public static final int KEY STAR

( ) , , Up (), Down
(), Left (), Right () Select () ,
, ,
MIDP. , , ,
6.3, .
6.3. Canvas, ,

Canvas

public static final int UP

public static final int DOWN

public static final int LEFT public


static final int RIGHT

public static final int FIRE

(]

6.4.
, , Canvas GAME_A,
GAME_B, GAME_C, GAME_D FIRE, ,
103

J2ME.
.
,
.
, ,
.
:
public int getKeyCode (int gameAction)
public int getGameAction(int keyCode)
6.2
.
, , .
getGameAction () 0. ,
GAME_A, GAME_B, GAME_C GAME_D. ,
, Motorola Accompli 008.

6.5.
, , , canvas, 6.2,
Exit (). , Canvasl
. Canvas
, - .
javax.microedition.lcdui.Graphics.
Graphics Canvas.

6.6.
Graphics .
AWT Swing, .
,
Graphics J2SE.
Graphics , -
Java - Canvas. public void
paint(Graphics g) Canvas,
protected abstract Canvas. Canvas paint(Graphics g) ,
, - .
Canvas Graphics. Graphics

,
.

104

Graphics, , Canvas
Canvas. , ,
Canvas super()! Canvas,
paint (Graphics g).

6.7. Graphics
Graphics :

;
;
;
(clipping);
Graphics.

, , . Display
:
public int isColorO
public int numColors()

-
, .
, Graphics, Canvas,
. 6.3
.
(, ), , ,
.

105

6.3. Graphics

6.8.
Graphics
:

;
;
;
.

Graphics
, .
, 6.3.
, .
. 6.4 , Canvas.

6.4. Canvas.
,
106

6.3 , 6.4, MID-,


.
http://www.phptr.com/. , , ,
, , MID- ,
, . ,
.
6.3. paint (), ,

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.raicroedition.lcdui.Command;
/*'
,
Graphics.
@ javax.microedition.Icdui.Graphics
*/
public class LineDemo extends Canvas .
implements CommandListener
}
// , .
private static final int WHITE = OxFF 16 | OxFF 8 I OxFF;
private Command back = new Command("Back", Command.BACK, 1);
private GraphicsDemo gDemo = GraphicsDemo.getlnstance(};
private Display display = Display.getDisplay(gDemo);
/**
No-arg.
*/
public LineDemo()
{
super ();
addCommand(back);
setCommandListener(this) ;
display.setCurrent(this);
}
/*'*
, ,
Canvas . "/
protected void paintdipRect (Graphics g)
}
int clipX = g.getClipX ();
int clipY = g.getClipY() ;
107

int clipH = g.getdipHeight () ;


int clipW = g.getClipWidth();
int color = g.getColor ();
g.setColor(WHITE);
g.fillRect(clipX, clipY, clipW, clipH);
g.setColor (color);
}
/ **
Canvas.
*/
public void paint (Graphics g)
{
paintdipRect (g) ;
int width = getWidth();
int height = getHeight ();
g.drawLine (20, 10, width - 20, height - 34);
g.drawLine(20, 11, width - 20, height - 33);
g.drawLine(20, 12, width - 20, height - 32);
g.drawLine(20, 13, width - 20, height - 31);
g.drawLine(20, 14, width - 20, height - 30);
g.setStrokeStyle(Graphics.DOTTED);
g.drawLine(20, 24, width - 20, height - 20);
g.drawLine(20, 25, width - 20, height - 19);
g.drawLine(20, 26, width - 20, height - 18);
g. setStrokeStyle (Graphics.SOLID);
g.drawLine(20, 36, width - 20, height - 8);
}
public void commandAction(Command c, Displayable d)
{
if (c == back)
{
GraphicsDemo.getlnstanceO.display() ;
}
}
}
paint (Graphics g) . Canvas
, . ,
6.2, , paint (Graphics g)
.
paint (Graphics g)
Graphics, . ,
Graphics, , Canvas.
, (, ) .
(, ) (0, 0), ,
108

, , ,
6.3.
0 ( ), 0,
.
. ,
, 6.3. ,
6.4, 6.3, ,
.
, .
setStrokeStyle (), .
, Graphics.DOTTED,
.
. : .
6.5 .

6.5. , ,
.
,
109

6.4 paint (Graphics g) .


6.4. RectangleDemo
. ,
import javax.microedition.lcdui.Canvas;
import javax.microedition.Icdui.Command;
import javax.microedition.Icdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.Icdui.Displayable;
import javax.microedition.Icdui.Graphics;
import javax.microedition.Icdui.Command;
/**
Canvas
javax.microedition.Icdui.Graphics.
@ javax.microedition.Icdui.Graphics
*/
public class RectangleDemo extends Canvas
implements CommandListener
{
// , .
private static final int WHITE = OxFF 16 | OxFF 8 I OxFF;
private Command back = new Command("Back", Command.BACK, 1);
private Display display =
Display.getDisplay(GraphicsDemo.get!nstance()) ;
/**
No-arg. no-arg Canvas.
*/
public RectangleDemo()
}
super () ;
addCommand(back); setCommandListener(this);
display.setCurrent (this) ;
}
/**
,
, Canvas .
*/
protected void paintClipRect(Graphics g)
{
int clipX = g.getClipX () ;
int clipY = g.getClipY();
int clipH = g.getClipHeight();
int clipW = g.getClipWidth ();
int color = g.getColor();
g.setColor (WHITE);
g.fillRect(clipX, clipY, clipW, clipH);
110

g.setColor (color);
}
/**
Canvas.
*/
public void paint(Graphics g)
{
paintClipRect(g);
int width = getWidthO; int height = getHeightf);
int xO = 5;
int yO = 5;
int barW = 10;
int initHeight = height - 10;
int deltaH = 10;
g.drawRect(xO, yO, barW, initHeight);
g.fillRect(xO + barW, yO + deltaH, barW, initHeight - deltaH + 1);
g.drawRect(xO + barW " 2, yO + deltaH * 2,
barW, initHeight - deltaH * 2);
g.setColor (255, 00, 00); g.fillRect(xO + bar * 3, yO + deltaH * 3,
barW, initHeight - deltaH * 3 + 1) ; g. setColor (0," 0, 0);
g.drawRect(xO + barW * 4, yO + deltaH * 4,
barW, initHeight - deltaH * 4);
g.fillRect(xO + barW * 5, yO + deltaH * 5,
barW, initHeight - deltaH * 5 + 1);
g.drawRect(xO + barW * 6, yO + deltaH * 6,
barW, initHeight - deltaH * 6); g.fillRect(xO + barW * 1, yO + deltaH * 1,
barW, initHeight - deltaH * 7 + 1);
}
public void commandAction(Command c, Displayable d)
{
if (c == back)
{
GraphicsDemo.getlnstanceO.display!) ;
}
}
}
. Graphics . ,
. ,
, .
,
.

. 0 X
. 6.6 , paint
(Graphics g) 6.5.
111

6.6. ,

6.5. ,

import javax.microedition.lcdui.*;
/**
Graphics.
javax.microedition.lcdui.Graphics
*/
public class ArcDemo extends Canvas
implements ComraandListener
{
public void paint(Graphics g)
{
paintClipRect(g);
}
int width = getWidth();
int height = getHeight ();
g.drawArc(5, 5, 80, 40, 90, 300);
112

g.fillArc(5, 60, 80, 40, 0, 250);


}
....
}
, f illArc
() drawArc ().
. Graphics Canvas. ,
6.4, Canvas,
Canvas.
6.4. Canvas, Canvas

Canvas

public void drawString(String str, int x, int


y, int anchor)

,
,
(, ]

public void drawSubstring(String str, int


offset, int len, int x, int y, int anchor)

,
, ,
,
(, )

public void drawChar (Char char, int x, int


y, int anchor)

,
(, )

,
, , , ,
6.7. ,
.
(, )
.
. ,
(, ).
6.7 ,
.
.
: . 6.5
Graphics. public static final int.

113

6.7. ,


6.5. -

static int LEFT

static int HCENTER


static int RIGHT

static int TOP

static int BASELINE


static int BOTTOM
static int VCENTER

Graphics ,
.
6.8 , Canvas, 6.6
paint (Graphics g) , .

114

6.8. , .
,
6.6. , .
,
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.rnicroedition.lcdui.CornmandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
/**
, Canvas.
Graphics.
javax.microedition.lcdui.Graphics
*/
public class TextDemo extends Canvas
implements CommandListener
}
115

public void paint(Graphics g)


}
paintClipRect(g) ;
int width = getWidth (); int height = "getHeight () ;
g.setFont(Font.getDefault Font());
g.drawStriny("Default", 5, 30, Graphics.LEFT I Graphics.BOTTOM);
g. setFont (Font.get Font (Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_LARGE)) ; g.drawstring("Large", 5, 53, Graphics.LEFT | Graphics.BOTTOM);
g.set Font(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC,
Font.SIZE_MEDIUM));
g.drawString("Medium", 5, 71, Graphics.LEFT I Graphics.BOTTOM);
g.set Font(Font.get Font(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED,
Font.SIZE_SMALL));
g.drawString("Small", 5, 90, Graphics.LEFT I Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_MEDIUM));
g.drawString ("V", width - 10, 20, Graphics.RIGHT I Graphics.BOTTOM)
g.drawStringC'E", width - 10, 32, Graphics.RIGHT I Graphics.BOTTOM)
g.drawString("R", width - 10, 44, Graphics.RIGHT I Graphics.BOTTOM)
g.drawStringC'T", width - 10, 56, Graphics.RIGHT I Graphics.BOTTOM)
g.drawString("I", width - 10, 68, Graphics.RIGHT I Graphics.BOTTOM)
g.drawString ("C", width - 10, 80, Graphics.RIGHT | Graphics.BOTTOM)
g.drawStringC'A", width - 10, 92, Graphics.RIGHT I Graphics.BOTTOM) g.drawString ("L", width - 10, 104,
Graphics.RIGHT I Graphics.BOTTOM);
g.drawChar('B', width - 25, 20, Graphics.RIGHT | Graphics.BOTTOM);
g.drawChar('0', width - 25, 32, Graphics.RIGHT I Graphics.BOTTOM) ;:
g.drawChar('L', width - 25, 44, Graphics.RIGHT I Graphics.BOTTOM) ;:
g.drawChar ( 'D', width - 25, 56, Graphics.RIGHT I Graphics.BOTTOM);
}
...
}
, Default, Large,
Medium Small, .
. , OR
(LEFT | BOTTOM) .
BOLD VERTICAL
drawChar(). .
RIGHT
,
.
API Graphics , VCENTER,
.
116

. VCENTER ,
(, ). .
. , Canvas,
6.6. , : ,
. javax.microedition.lcdui.Font
, 6.6.
6.6. ,

static int FACE MONOSPACE

static int FACE_PROPORTIONAL

static int FACE SYSTEM

static int STYLE BOLD

static int STYLE ITALIC

static int STYLE PLAIN

static int STYLE UNDERLINED

static int SIZE SMALL

static int SIZE MEDIUM

static int SIZE LARGE

, MIDP
, . , ,
.
AWT Swing,
. , Font final
public, .
MIDP .
Font ,
Graphics.setFontf). Font, static:
Font.getFont(int face, int style, int size)
Font.get Default Font ()

, . 6.6
.

117

6.9.
Display.setCurrent(),
Displayable. Canvas
paint (Graphics g).
, .
, paint() 6.1 API,
Canvas.

. - ,
,
. MID-
- , ,
SMS .
, Canvas ,
, .
, .
, , paintClipRect (Graphics g)
6.3. 6.7 . , paint (Graphics
g) . ,
.
6.7. ,
.
, ,

protected void paintClipRect(Graphics g)
int clipX = g.getClipX (); int clipY = g.getClipY() ;
int clipH = g.getClipHeight(); int clipW = g.getClipWidth();
int color = g.getColor();
g.setColor(WHITE);
g.fillRect(clipX, clipY, clipW, clipH);
g.setColor(color);
}
,
Graphics. ,
. (,
) Graphics, .
, Graphics:

118

int getClipHeight ()
int getClipWidth ()
int getClipX()
int getClipY()
paint (Graphics g) ,
. , ,
, ,
.
-
, ,
. ,
Canvas, .
, 6.7 ,
, .
.
( )
, .
Graphics.
, , .
.
.
, .
.
, , ,
Canvas, . ,
,
, , , Canvas .
void clipRect(int x, int , int width, int height)

, , - ,
Canvas. ,
, .
clipRect () .
:
setClipfint x, int , int width, int height)
Canvas ,
,
119

. , ,
Canvas.
,
, .
.
, ,
. , 6.7,
, ,
.
, ,
, 6.7,
. , ,
- .
, .
- Graphics. -
.
. ,
,
. ,
, , .
,
.

, ,
. Graphics.
,
.

6.10.

, (, ) ,
(0, 0). (0, 0) Graphics.
Graphics Canvas, , (, ),
.
Graphics .
Graphics , .
Graphics
void translate(int x, int )
,
Graphics. (0, 0) .
120

. 6.9 ,
, 6.8. Canvas.
Go Graphics ,
. 6.10 , Go
. , ,
paint (Graphics g) . ,
Graphics,
.
Graphics, , .
Go .
.

6.9. Canvas , Graphics, (0, 0),


121

6.10. .
Graphics,
6.8. , Graphics,
, Graphics,
import javax.microedition.Icdui.Canvas;
import javax.microedition.Icdui.Command;
import javax.microedition.Icdui.CommandListener;
import javax.microedition.Icdui.Display;
import javax.microedition.Icdui.Displayable;
import javax.microedition.Icdui.Graphics;
/**
Graphics Canvas.
( javax.microedition.lcdui. Graphics
*/ public class TranslationDemo extends Canvas
implements CommandListener
{
private final int WHITE = OxFF 16 I OxFF 8 | OxFF;
private GraphicsDemo gDemo = GraphicsDemo.getlnstance ();
private Display display = Display.getDisplay(gDemo);
122

private static Command back = new Command("Back", Command.BACK, 1);


private static Command go = new Command("Go", Command.SCREEN, 1);
private static final int ORIGINAL_STATE = 1;
private static final int TRANSLATED_STATE = -1;
// , private int x = 20;
// , private int = 20;
// . private int deltaX = 30;
// . private int deltaY = 30;
// , ,
// ,
private int state = ORIGINAL_STATE;
/**
.
*/
public TranslationDemo()
{
super () ;
addCommand(back);
addCommand(go);
setCommandListener (this);
display.setCurrent(this);
}
protected void paintClipRect(Graphics g)
{
int clipX = g.getClipX() ;
int clipY = g.getClipY() ;
int clipH = g.getClipHeight(); int clipW = g.getClipWidth() ;
int color = g . getColor();
g.setColor(WHITE);
g.fillRect(clipX, clipY, clipW, clipH);
g.setColor (color) ;
}
public void paint(Graphics g)
{
int w = 50;
int h = 50;
paintClipRect(g); g.fillRect(x, y, w, h);
}
// .
// Go, .
private void toggleState()
{
state = -state;
}
// . Canvas.
private void toggleTranslation()
123

}
if (state == ORIGINAL_STATE)
x = x + deltaX; = deltaY;
}
else
{
x = x - deltaX;
= - deltaY; 1 toggleState();
// paint()
// Canvas.
// , , repaint () ;
*/
public void commandAction(Command c, Displayable d)
{
if ( == back)
GraphicsDemo.getInstanced.display!);
}
else if (c == go)
{
toggleTranslation() ;
}
}
}
,
Graphics, .
Graphics ,
.

124

7. MIDP ( 7)
7.1.
RecordStore , .
7.4 RecordStore
.
7.4. RecordStore

void- closeRecordStore ( )

static void deleteRecordStore ( )

long getLastModif ied ( )

String getNameO

int getNumRecords ()

byte [] getRecordfint recordld)

byte [] getRecord(int recordld, byte [] buffer, int


offset)

byte [] getRecordSize (int recordld)

int getSizef)

( ),

int getSizeAvailable ( )

int getVersionf)

static String [] listRecordStores ()

,
MID-

static RecordStore openRecordStore (String


name, boolean createlfNecessary)

,
,

7.2.
MIDP
. MID-, ,
, .
, .
RMS API
, ,
. MID- .

125

7.3. RMS
RMS , 7.1,
. - ,
. .
32 .
-.

7.1. RMS ,
,
MID- MID-
. MID- ,
MID- . MID , MID-
, . ,
.
, MID- MID- ,
AMS MID-.
.
, ,
126

,
!
. ,
, 11.
RMS :

.
.
.
() .
.

ID ,
. ID
Java int. RMS -, , , ,
, - .

7.4.
byte []. RMS
.
.
, .
,
,
.

7.5.
RMS
, RMS. ,
.
MIDP.
RMS .
, , , ,
.
, , ,
, .
, .
, :

AddScreen.java;
127

AddressBook.java;
AddressBookMain.java;
DeleteAllConfirmationScreen.java;
PersistenceDemo.java;
RecordList.java;
SearchResultScreen.java;
SearchScreen.java.

Web- Prentice-Hall
http://www.phptr.com. PersistenceDemo.java MID-,
, . AddressBookMain.java
.
7.1 AddressBook.java.
RMS API ID-.
MID- AddressBook, , ,
addressbook.
7.1. AddressBook
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import Java.io.ByteArrayInputStream/
import java.io.ByteArrayOutputStream;
import Java.io.DatalnputStream;
import java.io.DataOutputStream;
import Java.io.lOException;
/**
.
, String String.
,
, ,
.
*/
public class AddressBook
private static final String RECORD_STORE_NAME = "address-book";
private RecordStore recordStore;
public AddressBook () throws RecordStoreException
super!);
recordStore = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
{
void close() throws RecordStoreException
{
128

try
{
recordStore.closeRecordStore();
}
catch (RecordStoreNotOpenException rsno)
{
}
}
/*
, .
@ RecordStore, . .
public RecordStore getRecordStore()
}
return recordStore;
/**
.
@param name .
@parara phone .
@ RecordStoreException, .
public void addRecord(String name, String phone)
throws RecordStoreException
}
ByteArrayOutputStreara baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try
dos.writeUTF(name); dos.writeUTF(phone);
}
catch (lOException ioe)
{
ioe.printStackTracef);
)
int id =
recordstore.addRecord(baos.toByteArray(), 0,
baos.toByteArrayO .lengthy-System, out. println ("Record id = " + id);
}
/**
RecordEnumerator,

.
*/
RecordEnumeration getMatchesByNarae(String matchKey)
throws RecordStoreNotOpenException
(
MacchAllNaraesFilter filter =
new MatchAllNamesFilter(matchKey);
AlphabeticalOrdering comparator =
new AlphabeticalOrdering();
129

return recordStore.enuraerateRecords(filter,
comparator, false);
}
/**
RecordFilter, ,
( )!)
2)
. true,
, false - .
*/
class MatchAllNamesFilter implements RecordFilter
{
String requestString;
public MatchAllNamesFilter(String matchKey) ;
requestString = matchKey;
}
public boolean matches(byte [] candidate)
{
ByteArraylnputStream bais =
new ByteArraylnputStream(candidate);
DatalnputStream dis = new DatalnputStream(bais);
Siring name = null;
try
}
name = dis.readUTF();
if (name.indexOf(requestString) == 0)
return true;
else
return false;
}
catch (lOException ioe)
{
ioe.printStackTrace!);
return true;
}
}
/**
RecordCornparator,
.
*/
class AlphabeticalOrdering implements RecordCoraparator
}
.
public AlphabeticalOrdering ()
(
)
130

public int compare(byte [] reel, byte [] rec2)


{
ByteArraylnputStream baisl =
new ByteArraylnputStream(recl);
DatalnputStream disl = new DatalnputStream(baisl);
ByteArraylnputStream bais2 =
new ByteArraylnputStream(rec2);
DatalnputStream dis2 = new DatalnputStream(bais2);
String namel = null; String name2 = null; try
namel = disl.readUTF ();
name2 = dis2.readUTF () ;
}
catch (lOException ioe)
ioe.printStackTrace();
}
if (namel == null II name2 == null) return 0;
int result = namel.compareTo(name2);
if (result < 0)
return RecordCornparator. PRECEDES;
else if (result == 0)
return RecordCoraparator.EQUIVALENT;
else
return RecordComparator.FOLLOWS;
}
}
/**
.

,
!
void deleteAHRecords ()
}
try
RecordEnumeration re =
recordStore.enumerateRecords(null, null, false);
while (re.hasNextElement())
*/
int id = re.nextRecordld();
recordStore.deleteRecord(id);
}
}
catch (RecordStoreException rse)
{
rse.printStackTracel);
}}
/**
, .
131

/**
String .
*/
public String getStatistics ()
{
int numRecords = 0;
int space = 0;
StringBuffer stats = new StringBuffer("Records:
*/
try
{
numRecords = recordStore.getNumRecords ();
space = recordStore.getSizeAvailable();
)
catch (RecordStoreException rse)
(
rse.printStackTrace();
}
stats.append(String.valueOf(nuraRecords));
stats.append("\n\n") ;
stats.append("Available bytes: ");
stats.append(String.valueOf(space));
return stats . toString();
}
}
, AddressBook RecordStore.
, . RecordStore
RMS.
.
AddressBook RecordStoreException, openRecordStore()
, . javax.microedition.rras
. 7.2 ,
RMS.

132

7.2. RMS ,
. javax.microedition.rms,
AddressBook ,
, :
void addRecord(String name, String phone)
void deleteAHReccrds ()
String ge-Scatistics ()
RecordEnumeration getAHRecords(String matchKey)

. ,
, RMS
MIDP.

7.6. byte [ ]
, ,
. ,
String, TextField,
5. , addRecord () String
.
,
. , ,
, API RecordStore .
addRecord () - Java
DatalnputStream, Java .
RecordStore.
RecordStore.addRecord() int, ID
. ID
. .
133

7.7.Cc
:

ID;
.

,
RecordStore:
byte [] getRecord(int recordld)
, , , ID , .
, , , , ID -
, addRecord ().
.
, , - ,
RecordStore. ,
ID , . ,
, , ,
.
RecordStore
RecordEnumeration
enumerateRecords(RecordFilter filter,
RecordComparator comparator,
boolean keepUpdated)
. 7.2
RecordList.Java. .
, , , ID .
7.2. ,
(ID)
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
134

import javax.microedition.rms.RecordStoreException;
import java.io.ByteArraylnputStream;
import Java.io.DatalnputStream; import Java.io.lOException;
/**
,
, .
AddressBook, MID-
MID-.
@ AddressBook
@ AddressBookMain
*/
public class RecordList extends List
implements CommandListener
{
private static Command go =
new command("Go", Command.SCREEN, 1);
private static Command back =
new Command("Back", Command.BACK, 1);
private Display display;
private static RecordList instance;
/**
.
@param title ,
List.
*/
public RecordList (String title)
superltitle, List.IMPLICIT);
instance = this;
PersistenceDemo pDemo = PersistenceDemo.getlnstance ();
display = Display .get-Display (pDemo) ;
addCommand(back); setCommandListener (this);
if (buildRecordList() <= 0) setTitle("No records found");
}
/""
.
.
@ .
*/
public static RecordList getlnstance()
}
return instance;
}
void display ()
{
display.setCurrent (this);
{
135

/**
, . .
,
. ,
.
<> , ,
.
(@ , ,
0, .
*/
int buildRecordList ()
{
AddressBook addressBook =
AddressBookMain.get Instance!).getAddressBook();
RecordStore recordStore = addressBook.getRecordStore();
int numRecords = 0; try
RecordEnuraeration re;
re = recordStore.enumerateRecords(null,
null, false);
if (re.numRecords() > 0)
{
ByteArraylnputStream bais = null;
DatalnputStreara dis = null;
String name = null;
while (re.hasNextElement())
byte [] record = re.nextRecord();
bais = new ByteArraylnputStream(record); dis = new DatalnputStrearn (bais ) ;
String strRec = new String(record);
name = dis . readUTFO ;
appendfname, null ;
numRecords++;
)
)
else
}
Alert a = new Alert("No records",
"No records found in record store", null,
AlertType.CONFIRMATION); a.setTimeout(Alert.FOREVER);
display.setCurrent (a, AddressBookMain.get Instance ());
})
catch (RecordStoreException re)
re.printStackTrace(); Alert a = new Alert("Error retrieving record",
"Error retrieving record.", AlertType.CONFIRMATION);
a.setTimeout(Alert.FOREVER); display.setCurrent (a, this);
catch (lOException ioe)
136

}
ioe.printStackTrace();
}
finally
{
return numRecords;
{
public void coramandAction(Command c, Displayable d)
if (c == back)
AddressBookMain.getlnstancel).display ();
}
}
}
buildRecordList() ,
, ,
. enumerateRecords () RecordEnumeration, .
hasNextRecord() nextRecord() while
List .
,
. , , , string,
String. ,
- Java DatalnputStream, API
Java.

7.8.
. ,

. ,
, .
enuraerateRecords() .
, ,
, .
, RecordFilter,
javax.microedition.rms. boolean matches
(byte [] candidate). RecordFilter
, .
enumerateRecords() ,
.
7.3 SearchScreen. Java. ,
, , .
137

7.3. , , ,
API AddressBook,
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.lcdui.TextField;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStoreException;
import Java.util.Enumeration;
import Java.util.Vector;
/**
,
.
,
.
*/
public class SearchScreen extends Form
implements CommandListener
{
private static Command go =
new Command("Go", Command.SCREEN, 1);
private static Command back = new Command("Back", Command.BACK, 1);
private static SearchScreen instance; private Display display;
private AddressBookMain addressBook; private TextField keyEntry;
/**
.
*/
public SearchScreen(}
(
super("Search for entry");
instance = this;
PersistenceDerao pDemo = PersistenceDemo.getlnstance () ;
display = Display .getDisplay (pDerno) ;
addressBook = AddressBookMain.getlnstance ();
keyEntry = new TextField("Enter name",
null, 20, TextFieid.ANY); append(keyEntry);
addCommand(go); addCommand(back);
setCoramandListener(this);
}
/**
.
.
/**
.
138

**/
public static SearchScreen getlnstance ()
return instance; ) void display!)
( display.setCurrentlthis) ;
}
/**
, .

SearchResultScreen. ,
, .
Vector .
*/
void displaySearchResults(Vector results)
SearchResultScreen screen =
new SearchResultScreen (results);
display. setCurrenJ: (screen) ;
)
, .
,
,
TextField "keyEntry".
AddressBook.getMatchesByName() ,
.
*/
Vector buildSearchResults()
{
AddressBook addressBook =
AddressBookMain.getInstance().getAddressBookf);
String matchKey = keyEntry.getString(); Vector results = new Vectorf);
try
{
RecordEnuraeration re =
addressBook.getMatchesByName(matchKey);
byte [] record = null;
while (re.hasNextElement())
record = re.nextRecord () ; results.addElement(record);
}
}
catch (RecordStoreException rse)
}
rse.printStackTracet) ;
)
return results;
)
/**
.
class BuildSearchResultsAction implements Runnable
139

{
public void run ()
Vector results = buildSearchResults ();
displaySearchResults(results) ;
}
}
public void commandAction(Command c, Displayable d) ;
if (c == go)
Runnable action = new BuildSearchResultsAction();
action.run () ;
)
else if (c == beck)
}
AddressBookMain.getInstanced.display!);
}
}
}
buildSearchResults() SearchScreen ,
getMatchesByName (String matchKey) AddressBook.
, matchKey.
getMatchesByName () ,
enumerateRecords (). MatchAllNamesFilter
, matchKey.
enumerateRecords ()
:
boolean matches(byte [] candidate)
true, .
SQL . RecordFilter
.
, 7.2 RecordFilter .
RecordList , .
.
7.4, ,
RecordFilter ,
.

7.9.
, , , , enumerateRecords ()
, .
140

. - ,
.
.
, .
RecordComparator,
int ccmparefbyte [] recordl, byte [] record2)
, 7.1,
.
7.1. RecordComparator

public static int


EQUIVALENT

public static int FOLLOWS

1 , 2,

public static int PRECEDES

1 , 2,

.
, javax.microedition.rras.RecordComparator.
enumerateRecords (). , ,
, ,
. ,
.
7.4 .
AddressBook, 7.1.
AlphabeticalOrdering RecordComparator.
( ).
7.4. ,

/*'*
RecordComparator,
.
*/
class AlphabeticalOrdering implements RecordComparator
/**
No-arg.
*/
public AlphabeticalOrdering()
141

}
super(); )
public int comparelbyte [] reel, byte [] rec2)
ByteArraylnputStream baisl =
new ByteArraylnputStream(reel);
DatalnputStream disl = new DatalnputStream (baisl);
ByteArraylnputStream bais2 new ByteArraylnputStream(rec2);
DatalnputStream dis2 = new DatalnputStream(bais2);
String namel = null;
String name2 = null; try
(
namel = disl.readUTF ();
name2 = dis2.readUTF () ;
catch (lOExceotion ioe)
ioe.pnntStackTrace () ;
}
if (namel == null I| name2 == null) return 0;
int result = namel.compareTo(narae2);
if (result < 0)
return RecordComparater.PRECEDES;
else if (result == 0)
return RecordComparator.EQUIVALENT;
else
return RecordComparator.FOLLOWS;
}
}

, . ,
, ,
enumerateRecords (). ,
7.5, getMatchesByName(String
matchKey) AddressBook.
7.5. ,
.

RecordEnumeration getMatchesByName(String matchKey)
throws RecordStoreNotOpenException
{
MatchAllNaraesFilter filter =
new MatchAHNamesFilter (matchKey) ;
AlphabeticalOrdering comparator =
new AlphabeticalOrdering();
return recordStore.enumerateRecords(filter,
142

comparator, false) ;
}
, ,
, .
, List
. null ,
AlphabeticalOrdering .

7.10.

,
. RecordStore
,
7.2. ,
RecordListener, javax.microedition.rms.
, 7.3.
7.2. RecordStore
RecordStore

Void addRecordListener
(RecordListener listener)

Void removeRecordListener
(RecordListener listener)

7.3. RecordListener
RecordListener

void recordAdded (RecordStore


recordStore, int recordld)

,

ID

void recordChanged (RecordStore


recordStore, int recordld)

,
ID

void recordDeleted(RecordStore
recordStore, int recordld)

,
ID

,
,
.
,
.
,
, .
143

144

8. MIDP ( 8)
8.1. MIDP
MIDP, J2SE, - ,
, . J2SE, J2ME java.io,
. , MIDP javax.microedition.io,
MIDP.
java.net J2SE,
.
MIDP javax.microedition.io
.
java.io MIDP,
java.io J2SE.
, MIDP
,
.
.

8.2.Cpyypa MIDP
MIDP ,
, .
(connector)
. ,
. ,
.
, .
,
.
,
.
.
, ,
. , ,
, ,
.
MIDP
. J2SE, : ,
, , .

145

, URL J2SE, Java,net.URL,


URL. ,
URL, URL-.

, .
,
.
URL , . ,
, URL, ,
URL. , URL
.
, J2SE ,
, - URL, , , .
J2SE .
MIDP , J2SE,
. , ,
, . , ,
.
. -,
. -,
.
MIDP
,
. ,
.
, MIDP ,
, (universal
resource identifier (URI)), URI ,
RFC 2396. URI
. URI
<>://<;<>
URI , ,
. RFC 2396 , , as file, datagram,
socket, serversocket, http, ftp .
CLDC - . ,
CLDC . CLDC
. MIDP, ,
, . MIDP ,

146

, , HTTP 1.1.
MIDP:

,
,
.
( )
.
,
.
HTTP
.

MIDP .
MIDP CLDC.
, 1.

8.3.
8.1 ,
. , ,
, 8.1.

, . 8.1
.

8.1. ,
URI

147

1. Connector .
2. Connector.open() URI Connection.
Connection
.
3. InputStream OutputStream Connection.
4. InputStream OutputStream
.
5. Connection
,
.

;
- .

, HTTP-,
HTTP, . HTTP
,
. , ,
HTML, HTML .
, ,
, .
MIDP ,
.
, .

. , HTTP- MIDP.
, HTTP HTTP-.

8.4. cpyyp
javax.microedition.io ,
. Connector
.
. ,
.
,
, .
,
.
8.2 MIDP,
.

148

8.2. ,
.
, ,
. javax.microedition.io
Connection. ,
, . ,
. Connection -

public void close ()


, Connector,
n(). , ,
.
Connection .
,
. InputConnection
InputStream, . 8.1
.
8.1. InputConnection
InputConnection

DatalnputStream
openDatalnputStream ( )

DatalnputStream,
,

InputStream openlnputStreamf)

InputStream,
,

InputStream. , DatalnputStream
InputStream. , ,
.
149

, - ,
.
OutputConnection Connection.
.
8.2.
.

,
Datalnput DataOutput. , ,
, , , .
, ,
InputConnection OutputConnection.
8.2. OutputConnection
OutputConnection

DataOutputStream
openDataOutputStream ()

DataOutputStream,
, .

OutputStream
openOutputStream()

OutputStream,
, .

8.5.
StreamConnection InputConnection
OutputConnection. , 8.1 8.2.
StreamConnection
- . ,
. ,
,
InputConnection OutputConnection. ,
, .
StreamConnection , ,
. , J2ME Wireless Toolkit StreamConnection , Unix.
StreamConnection
. , ,
. ,
. , .
, ,
URI Connector.open(). ,
150

. ,
.
:
address := <>:<>;
<> cheme := "coram"
unit := <integer, eomn>
parameters := < >
,
:
StreamConnection conn = Connector.open("comm:0;baudrate=9600");
, ,
, , , , ,
.

8.6.
ContentConnection StreamConnection.
. , ,
,
(priori).
, ,
. ,
ContentConnection, , ,
,
, . ContentConnection
,
.
ContentConnection
, ,
. , ContentConnection ,
. 8.3
, ContentConnection. ,
.
8.3. ContentConnection

ContentConnection

String getEncoding ()

, ,

151

long getLength()

String getType()

, ,
, , .
HTTP.
, ContentConnection , HttpConnection,
, HTTP. HttpConnection
MIDP, CLDC. HTTP
. , , , ContentConnection,
8.3, HTTP.
HttpConnection
HTTP. ,
HTTP , .
.
8.4 HttpConnection.
8.4. HttpConnection

HttpConnection

long getDate ( )

long getExpiration ()

Expires

String getFilef)

URL

String getHeaderFieldfint n)

String getHeaderField (String


name)

.

HTTP

long getHeaderFieldDate (String ( )


name, long def)

int getHeaderFieldlnt (String
name, int def)

( ]

String getHeaderFieldKey (int n)


String getHostf)

HOST URL

long getLastModif ied()

LastModified URL.

int getPortf)

URL

String getProtocol ()

URL

String getQueryO

URL, "?" URL

String getReff)

URL
152

String getRequestMethod ()

String getRequestProperty
(String key)

int getResponseCode()

v HTTP

String getResponseMessage ( )

HTTP,

String getURLO

URL

void setRequestMethod (String


method)

URL;
GET, POST HEAD

void setRequestProperty (String



key, String value)
HttpConnection
, HTTP, 8.5.
HTTP 1.1,
RFC2616, http://www.w3c.org
http://www.ietf.org.
8.5. HttpConnection
HttpConnection

static String GET

GET

static String HEAD

HEAD

static int HTTP_ACCEPTED

HTTP 202

static int HTTP_BAD_GATEWAY

HTTP 502

static int HTTP_BAD_METHOD

HTTP 405

static int HTTP_BAD_REQUEST

HTTP 400

static int HTTP_CLIENT_TIMEOUT

HTTP 408

static int HTTP_CONFLICT

HTTP 409

static int HTTP_CREATED

HTTP 201

static int HTTP_ENTITY_TOO_LARGE

HTTP 413

static int HTTP_EXPECT_FAILED

HTTP 41 7

static int HTTP_FORBIDDEN

HTTP 403

static int HTTP_GATEWAY_TIMEOUT

HTTP 504

static int HTTP_GONE

HTTP 410

static int HTTP_INTERNAL_ERROR

HTTP 500

static int HTTP_LENGTH_REQUIRED

HTTP 41 1

static int HTTP_MOVED_PERM

HTTP 301

static int HTTP_MOVED_TEMP

HTTP 302
153

static int HTTP_MULT_CHOICE

HTTP 300

static int HTTP_NO_CONTENT

HTTP 204

static int HTTP_NOT_ACCEPTABLE

HTTP 406

static int HTTP_NOT_AUTHORITATIVE

HTTP 203

static int HTTP_NOT_FOUND

HTTP 404

static int HTTP_NOT_IMPLEMENTED

HTTP 501

static int HTTP_NOT_MODIFIED

HTTP 304

static int HTTP_OK

HTTP 200

static int HTTP_PARTIAL

HTTP 20

static int HTTP_PAYMENT_REQUIRED

HTTP 402

static int HTTP_PRECON_FAILED

HTTP 412

static int HTTP_PROXY_AUTH

HTTP 407

static int HTTP_REQ_TOO_LONG

HTTP 414

static int HTTP_RESET

HTTP 205

static int HTTP_SEE_OTHER

HTTP 303

static int HTTP_TEMP_REDIRECT

HTTP 307

static int HTTP_UNAUTHORIZED

HTTP 401

static int HTTP_UNAVAILABLE

HTTP 503

static int HTTP_UNSUPPORTED_RANGE

HTTP 416

static int HTTP_UNSUPPORTED_TYPE

HTTP 41 5

static int HTTP_USE_PROXY

HTTP 305

static int HTTP_VERSION

HTTP 505

static String_HTTP_POST

POST

, HttpConnection
. HTTP
, MIDP.
8.1 8.4 , ,
HTTP .
,
, ,
-. URI ,
.
HTTP , ,
-. URI ,
, -,
. URI,
URI, -. ,
154

URI, URI,
. , ,
8.1, lOException.
, 8.1,
.
, HEAD HTTP. ,
, , , ,
. , HTTP, MIDP,
.
8.1. ConnectionDemo MID-, HTTP, HTTP.
HEAD ,
import javax.microedition.midlet.MI Diet;
import javax.microedition.lcdui.Display;
MID- ,
URI, HTTP
.
Form, URI.
*/
public class ConnectionDemo extends MID-
}
private static ConnectionDemo instance;
private URIEntry urlForm; public ConnectionDemo()
super(); instance = this; }
/**
.
.
@ ,
public static ConnectionDemo getlnstance ()
return instance;
}
public void startApp()
Display display;
URIEntry urlForm = URIEntry.getlnstance();
display = Display.getDisplay(this); display.setCurrentlurlForm);
}
public void pauseApp()
}
}
void quit ()
destroyApp(true); notifyDestroyedf) ;
}
public void destroyApp(boolean destroy)
155

{
instance = null;
/**

MID- .
*/
public void display()
Display.getDisplay(this).setCurrent(urlForm);
}
}
8.2. URIEntry , URI
import: javax.micrcedition.midlet.MIDlet;
import javax.microedition.Icdui.Command;
import javax.microedition.Icdui.CommandListener;
import javax.raicroedition.Icdui.Display;
import javax.microedition.Icdui.Displayable;
import javax.microedition.Icdui.Form;
import javax.microedition.Icdui.TextField;
/**
Form, URI,
HTTP.
URI Go.
ResourceDisplay,
HTTP .
*/
public class URIEntry extends Form implements CommandListener
}
private static Command go =
new Command("Go", Command.SCREEN, 1);
private static Command exit =
new CommandCExit", Command. EXIT, 1) ;
private static URIEntry instance;
// URI, , private TextField uri;
// ,
// ResourceDisplay. private Thread thread;
/**
.
@param title Form.
*/
private URIEntry(String title)
}
156

super(title); instance = this;


uri = new TextField. ("Connect to:",
null, 70,
TextField.URL); uri.setStringf'http://") ; append (uri) ;
addCommand(go); addCommand(exit); setCommandListener(this);
}
/**
.
^ .
*/
public static URIEntry getlnstance ()
}
if (instance == null)
{
instance = new URIEntry("Enter URL");
}
return instance;
}
/**

MID-.
*/
public void display()
MIDlet = ConnectionDemo.getInstance();
Display.getDisplay(m).setCurrent(this);
}
public void commandAction(Command c, Displayable d)
}
if (c == go)
}
// ,
// URI.
ResourceDisplay view =
new ResourceDisplay(uri.getString());
MIDlet m = ConnectionDemo.getInstar.ee ();
Display.getDisplay(m).setCurrent(view);
thread = new Thread(view); thread.start();
}
else if (c == e\it)
}
ConnectionDemo.getlnstance().quit();
}
}
}

157

8.3. ResourceDisplay , .
helper
import javdx.microedition.lcdui.Command;
import javax.microedition.Icdui.CommandListener;
import javax.microedition.Icdui.Form;
import javax.microedition.Icdui.Displayable;
/**
Form, ,
HTTP-. ,
Runnable.
Form helper HTTP-
Connection.
helper .
public class ResourceDisplay extends Form
implements CommandListener, Runnable
{
private static Command back =
new Command("Back", Command.BACK, 1);
private static Displayable instance;
// helper
// .
// private HttpResource resource;
.
Sparam uri URI HTTP .
*/
public ResourceDisplay(String uri)
{
super("Http Info");
instance = this;
resource = new HttpResource(uri);
addCommand(back);
setCommandListener(this);
}
/**
:
helper HttpResource.
@ . rtpResource
*/
public void run()
{
resource.run();
append(resource.getResourceMetalnfo());
}
/**
.
.
158

@ .
*/
public static Displayable getlnstance ()
{
return instance;
{
public void commandAction(Command c, Displayable d)
{
if (c == back)
{
URI Entry, get Instanced .display();
}
}
}
8.4. HttpResource ,

import Java.io.InputStream;
import Java.io.lOException;
import javax.microedition.io.Connect ion;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.Icdui.Displayable;
/**
helper,
ResourceDisplay. HTTP,
.
. ,

String .
.
J2MEWTK.
, Runnable.

, ,
.

, ResourceDisplay,
, .
**/
public class HttpResource implements Runnable
private static Displayable instance;
159

// URI, .
private String uri;
// .
private StringBuffer contents = new StringBuffer();
// . private Connection conn;
// HTTP-, private HttpConnection httpConn;
// , private InputStream is;
// HTTP. private int status = -1;
/**
.
@pararc uri URI, .
*/
public HttpResource (String uri)
{
super ();
this.uri = uri;
}
private String userAgentID ()
{
StringBuffer buf = new StringBuffer();
String config =
System.get Property("microedition.configuration");
String profile =
System.get Property("microedition.profiles");
buf.append("Configuration/"); buf.append(config); buf.append!" Profile/");
buf.append(profile); return buf . toStrir.g () ; )
/**
. URI,
, .
*/
public void run()
System.out.println("Connection class name = " + conn.getClass().getName ());
connect () ; parse () ;
System.out.println(gecResourceMetalnfo() ) ;
try conn.close();
}
catch (lOException ioe) System.out.println(ioe.getMessage()) ;
ioe.printStackTrace();
}
}
/**
, URI.
,
,
.
*/
protected void connect!)
160

}
try
}
while (true)
{
// . conn = Connector.open(uri);
httpConn = (HttpConnection) conn;
httpConn.setRequestProperty("method", HttpConnection.HEAD);
httpConn.setRequestProperty("User-Agent", userAgentID());
// . if (resourceRelocated())
{
uri = httpConn.getHeaderField("location");
//
// close().
conn.close();
}
else
}
breaX;
*/
if (serverError())
{
conn.close () ; return;
}
// , is = httpConn.openlnputStream ();
System.out.println("Input stream class name = " + is.getClassO .get Name () ) ;
int responseCode = httpCcnn.getResponseCode ();
printResponseCode (responseCode) ; catch (lOExceptior. ioe)
{
contents.append(ioe.getMessage());
System.out.println(ioe.getMessage());
ioe.printStackTrace() ;
}
}
private boolean resourceRelocated()
{
boolean relocated = false; try
}
status = httpConn.getResponseCode();
if (status == HttpConnection.HTTP_MOVED_TEMP II
status == HttpConnection.HTTP_MOVED_PERM II
status == HttpConnection.HTTP_TEMP_REDIRECT)
{
relocated = true;
}
}
catch (lOException ioe)
161

}
System.out.println(ioe.getMessage() ) ;
ioe.printStackTrace() ;
}
return relocated;
}
private boolean serverError ()
{
boolean error = false;
try
{
status = httpConn.getResponseCode();
if ((status == HttpConnection.HTTP_NOT_IMPLEMENTED)
If (status == HttpConnection.HTTP_VERSION)
If (status == HttpConnection.HTTP_INTERNAL_ERROR)
If (status = = HttpConnection.HTTP_GATEWAY_TIMEOUT)
If (status == HttpConnection.HTTP_BAD_GATEWAY))
}
error = true; } }
catch (lOException ioe)
{
error = true;
System.out.println(ioe.getMessage()) ;
ioe.printStackTrace() ;
}
return error;
}
private void parse()
(
if (httpConn == null) return;
String protocol = httpConn.getProtocol();
contents.append("Protocol: " t protocol + "\n");
String type = httpConn.getType();
content's . append ("Type : " + type + "\n");
String encoding = httpConn.getEncoding ();
contents.append("Encoding: " + encoding + "\n");
long length = httpConn.getLength ();
contents.append("Length: " + length + "\n");
String uri = httpConn.getURL();
contents.append("URL: " + uri + "\n");
String host = httpConn.getHost();
contents.append("Host: " + host + "\n");
String query = httpConn.getQuery();
contents.append("Query: " + query + "\n");
String requestMethod = httpConn.getRequestMethod();
contents.append ("Method: " + requestMethod + "\n");
}
162

private void printResponseCode(int code)


{
System.out.print("Response code :
**/
switch (code) case HttpConnection.HTTP_ACCEPTED:
Systern.out.print In("HTTP_ACCEPTED"); break;
case HttpConnection.HTTP_BAD_GATEWAY:
Systern.out.print In("HTTP_BAD_GATEWAY"); break;
case HttpConnection.HTTP_BAD_METHOD:
Systern.out.print In("HTTP_BAD_METHOD") ; break;
'case HttpConnection.HTTP_BAD_REQUEST:
Systern.out.print In("HTTP~BAD_REQUEST"); break;
case HttpCo-.nection.HTTP_CONFLICT:
System.out.println("HTTP_CONFLICT"); break;
case HttpConnection.HTTP_CREATED:
System.out.print In("HTTP_CREATED"); break;
case HttpConnection.HTTP_FORBIDDEN:
System.out.print In("HTTP_BAD_FORBIDDEN"); break;
case HttpConnection.HTTP_GATEWAY_TIMEOUT:
System.out.print In("HTTP_GATEWAY_TIMEOUT"); break;
case HttpConnection.HTTP_GONE:
Systern.out.print In("HTTP_GONE"); break;
case HttpConnection.HTTP_NO_CONTENT:
System.out.println("HTTP_NO_CONTENT"); break;
case HttpConnection.HTTP_NOT_ACCEPTABLE:
Systern.out.print In("HTTP_NOT_ACCEPTABLE"); break;
case HttpConnection.HTTP_NOT_FOUND:
System.out.print In("HTTP~NOT_FOUND"); break;
case HttpConnection.HTTP_OK:
System.out.println("HTTP_OK"); break;
case HttpConnection.HTTP_PROXY_AUTH:
Systern.out.print In("HTTP_PROXY_AUTH"); break;
case HttpConnection.HTTP_UNAVAILABLE:
Systern.out.print In("HTTP_UNAVAILABLE"); break;
case HttpConnection.HTTP_VERSION:
System.out.print In("HTTP_VERSION"); break; default:
System.out.println (); ;. }
/**
.
@ ,
.
*/
public String getResourceMetalnf()
}
return contents.toString();
}
}
163

, 8.1 - 8.4:

ConnectionDemo MID- .
URIEntry.
URIEntry , URI,
.
ResourceDisplay ,
.
HttpResource helper, ResourceDisplay
.

ConnectionDemo MID-. (
URIEntry), URI. HttpResource
, .
ResourceDisplay . HttpResource -
.
.
. URI
URIEntry. URIEntry ResourceDisplay ,
, : .
. URIEntry
ResourceDisplay.
ResourceDisplay HttpResource
. .
:

HttpResource;
;
, ;
;
.

. ,
, MIDP ,
, -
.
. -
commandAction().
, , ,
HTTP. , CommandListener
commandActionO . , , 8.1,
Connector.open() , .
15 J2MEWTK. ,

164

MIDP -
.
HttpResource API, .
Runnable run().
,
run() ResourceDisplay, HttpRespource.run().
HttpResource , , , Runnable
.
. ,
.
, .
,
.
Connector . ,
Connector.open() .

(Design Patterns) Gamma et al.. Connector
,
. URI - - , ,
, .
, ,
.
, .
,
. , , 8.4,
HttpConnection. ,
HttpResource.connect ().
Connection conn;
HttpConnection httpConn;
...
conn = Connector.open(uri);
httpConn = {HttpConnection) conn;
....
. URI http.
Connection, HttpConnection.
, - HttpConnection. ,
, HttpConnection, a Connection.
,
Connector.n().

165

, HttpResource.run(),
, HttpConnection:
public void run ()
System.out.println("Connection class name = " +
conn.getClass() . get Name () ) ;
connect () ;
parse () ;
...
}

Sun J2ME Wireless Toolkit, ,


, J2ME
Sun, Sun J2ME Wireless Toolkit:
com.sun.midp.io.j2me.http.Protocol
, 8.1 - 8.4,
, .
HttpConnection.
.
HTTP. HTTP
. HTTP. :

- , .
- ,
, .
- .
lOException.

8.3
HTTP. .
. ,
HTTP . ,
8.6. , , , -
HTTP, . , ,
.
, ,
8.7.
, , , ,
. , , 8.7,
166

. ,
, .

8.3. HttpConnection

8.6. HttpConnection HTTP


HttpConnection

void setRequestMethod (String


method)

HTTP, HEAD, POST,


GET

void setRequestProperty (String key,


String value)

,
value

8.7. HttpConnection,

HttpConnection

InputStream openlnputStream ()

InputStream (
InputConnection)

OutputStream openOutputStream()

OutputStream
( OutputConnection)

DatalnputStream
openData!nputStream( )

DatalnputStream (
InputConnection)

DataOutputStream

DataOutputStream (
167

openDataOutputStream()

OutputConnection)

long getDate()

date

String getEncoding ()

,
( ContentConnection]

long getExpiration ( )

expires

String getHeaderField (String name)


long getHeaderFieldDate (String
name, long def)

String getHeaderFieldlnt (String


name, int def)

String getHeaderFieldKey (int n)

long getLastModif ied ( )

last-modified

long getLength()

int getResponseCode ( )

HTTP

String getResponseMessage ( )

HTTP

String getType()

,
( ContentConnection)

,
. , 8.7 8.9.
, 8.8, HTTP,
close (), .
,
, 8.6. ,
,
URI. , URI
Connector.(). ,
, .
, 8.4 ,
, .
HTTP
HTTP.
8.8. HttpConnection,
HttpConnection

void close ()

(
Connection)

String getFile()

<file> URL
168

String getHostO

<host> URL

int getPortO

<port> URL

String getProtocol ()

<protocol> URL

:" i ing getQuery ()

URL

String getRequestMethodf)

(GET, POST )

String getRequestProperty (String key)

String getRef()

<ref> URL

String getURL()

URL

. ,
, ,
,
.
HTTP MIDP. HTTP

.
,
MIME.
HTTP , ,
HTML XML. , HTTP
. , ,
-.
HTTP .
, ,
HTTP .
Java, Java Server Pages (JSP), Pearl scripts, CGI .
, Java
Java enterprise,
. ,
Web- . 11
,
.

8.7.
javax.microedition.io.DatagramConnecti.on Connection.
, 8.2, ,
, ,
.
169

DatagramConnection ,
.
.
. , ,
.
,
OSI, . ,
.
, ,
. , ,
.
.
- (Internet Universal Datagram Protocol
(UDP)) .
, MIDP.
- (Internet Protocol (IP)) . ,
MIDP, HTTP 1.1 ,
, - .
.
UDP MIDP
. 11
,
, .
UDP , , , ,
TCP, , , ,
, , ,
. .
MIDP ,

.
8.9 DatagramConnection. ,
.
. HttpConnection,
HTTP
MIME . ,
, HTTP, ,
, .
8.9. DatagramConnection

170


DatagramConnection

int getMaximumLength ( )

int getNominalLength ( )

Datagram newDatagram(byte [] buf, int ,


size)

Datagram newDatagram(byte [ ] buf,
int size, String addr)

Datagram newDatagramfint size)

Datagram newDatagram (int size,


String addr)

void receive (Datagram dgram)

void send (Datagram dgram)

, -
:
1. DatagramConnection.
2. Datagram DatagramConnection.
3. Datagram , ,
.
4. .
5. .
, Connector.
, ,
URI,
Connector.open(). :
address := <>://<>
protocol := "datagram"
target := [<>]:<>
host := DNS- >
port := >
. ,
- , , ,
. ,
. .
, ,
. :

171

datagram:/7:513
, .
, , ,
. ,
:
datagram://server.foo.com:513
,
. javax.microedition.io.Datagram ,
,
. DatagramConnection Datagram.
, , 8.9, Datagram.
8.10 Datagram. ,
:

- ;
-
, .

, .
, .
Datagram .
,
, .
.
byte [].
.
.
, . byte []

.
Datagram Datalnput DataOutput java.io.

. 8.4
Datagram. 8.11 Datalnput, 8.12
DataOutput.
java.io J2SE.

172

8.4. .
,
. , ,

8.10. Datagram

Datagram

String getAddress ()

byte [] getData()

int getLength()

int getOffsetO

void reset()

void setAddress (Datagram


reference)

void setAddress (String addr)

void setData (byte[] buffer, int


offset, int len)

void setLength (int len)

,
.
. , , ,
.
8.11. Datalnput
Datalnput

boolean readBoolean ( )

Boolean

byte readByte()


173

char readCharf)

void readFully (byte [] b)

void readFully(byte[] b, int off, int ,


len)

int readlnt()

int

long readLong()

long

short readShort()

short

int readUnsignedByte()

, ,

int readUnsignedShort ()

int

String readUTF()

UTF-8

int skipBytes (int n)

8.12. DataOutput

DataOutput

void writeByte (byte [ ] b)


void write (byte[] b, int
off, int len)

void write (int b)

void writeBoolean
(boolean v)

boolean

void writeByte (int v)

int

void writeChar (int c)

void writeChars (String s)

void writelnt(int v)

int ( )

void writeLong (long v)

long ( )

void writeShort (int v)

int

void writeUTF(String s)

Java LJTF,
,

, MIDP
Unix (Unix Network Time Protocol (NTP)),
, NTP, 123.
- MIDP ,
NTP. ,
.

174

MIDP J2SE .
J2SE java.net. , , DatagramPacket .
DatagramSocket .
CLDC/MIDP. java.net
CLDC/MIDP. , CDC java.net, .
8.5 . , ,
,
. , , :
1. DatagramConnection.
2. Datagram DatagramConnection.
3. Datagram ,
( , ,
, ).
4. Datagram DatagramConnection.
, .
5. .
6. .
, 8.5, 3.
, ,
. , 5,
.
.
8.5. .

import javax.microedition.midlet.MIDlet;
import javax.microedition.Icdui.Display;
import javax.microedition.Icdui.Command;
import javax.microedition.Icdui.CommandListenerj;
import javax.microedition.Icdui.Displayable;
import javax.microedition.Icdui.TextBox;
import javax.microedition.Icdui.TextFie Id;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import Java.io.lOException; ft,
,

(NTP) NTP 123.
,
Runnable.

175

.
, .
NTP . ,
MIDP.
*/
public class DatagramTest extends MIDlet,
implements CommandListener, Runnable
}
private static final int BUF_SIZE = 1024;
private static Command exit =
new Command ("Exit", Command.EXIT, 1);
private static DatagramTest instance; private Display display;
private TextBox dgramText;
// . private DatagramConnection conn;
// ,
, private Datagram dgram;
// (NTP)
// . NTP
UDP. private String address = "datagram://srl-usca28-07:123";
/"*
No-arg.
*/
public DatagramTest()
{
super (); instance = this;
}
/**
.
,
. ,
.
@param service URI , .
*/
public DatagramTest(String service)
(
this ();
address = service;
}
/**
.
.
@ .
*/
public static DatagramTest getlnstance()
}
176

return instance;
{
public void startApp()
}
display = Display.getDisplay (this);
dgramText = new TextBox("Datagram contents", null, 2048,
TextField.ANY); dgramText.setCommandListener (this);
display.setCurrent(dgramText); run ();
}
/*
.
.
.

. ,
.
*/
public void run ()
}
try int maxLength;
// ,
conn = (DatagramConnection) Connector.open(address);
maxLength = conn.getMaximumLength();
dgram = conn.newDatagram(maxLength);
// , /
// ,
// , dgram.reset();
// ,
// .
// .
**/
// . conn.send(dgram);
// ;
// . conn.receive(dgram);
//
// Java,
// , , String.
// .
byte [] data = dgram.getData ();
// . String str = new String (data);
// .
// . System.out.println(str);
// /
// . ,
// ,
// .
177

// ,
//
// .
dgram.reset();
// , ,
// .
// ....
}
catch (lOException ioe)
(
System.out.println(ioe.getMessage() ) ;
loe.printStackTrace();
quit();
}
return;
}
public void pauseApp()
{
}
void quit()
destroyApp(true); notifyDestroyedf); }
public void destroyApp(boolean destroy) }
try }
conn.close () ;
}
catch (lOException ioe) ioe.printStackTracef) ;
public void display!)
Display.getDisplay(this).setCurrent(dgramText); )
public void commandAction(Command c, Displayable d)
{
if (c == exit)
}
quit();
}
}
}
, Datagram ,
DatagramConnection.
Datagram.
Datagram .
, ,
, .
178

, ,
.

. , ,
.
Datagram.reset()
/ .
Datagram , , ,
. Datagram, ,
,
. reset () /
0. ,
, , .

8.8. coea
,
MIDP. MIDP, ,
UNIX. ,
- , HTTP 1.1.
.
StreamConnectionNotifier .
StreamConnection, , .
- ,
TCP/IP . ,
.
,
. , .
, ,
. , .
.
, TCP. TCP
,
.
TCP/IP. , TCP/IP
, ,
, -
. ,
.

179

TCP/IP . ,
.
, TCP/IP,
- .
, ,
.
,
.
. , HTTP .
MIDP ,
, . ,
HTTP.
,
.
. ,
, Connector.open() URI
, .
- .
,
.
, ,
. , Unix.
:
1. , , .
2. .
3. .
4. ,
. ,

.
5. .
, , .
6. .
7.
.
8.5 .
, 8.5.

180

8.5.
.

,
.
- , . ,
, , .
,
, .
.
.
, .
.
.
URI Connector.open() . LJRI
:
address := <>://<>
protocol := "socket"
target := [<>]:<>
host := DNS- >
port := >
181

, URI ,
.
, :
StreamConnectionNotifier wellKnown =
Connector.open("socket://:98765");
MIDP serversocket .
, serversocket
-, , ,
. serversocket:
StreamConnectionNotifier wellKnown =
Connector.open("serversocket://:98765");
StreamConnectionNotifier MIDP Java.net.Serversocket
J2SE. StreamConnectionNotifier .
,
.
, :
StreamConnection clientConn = vellKnovn.acceptAndOpen();
, .
acceptAndOpen ()
. ,
1.
2.
3.
4.

;
;
;
.

StreamConnectionNotifier.
acceptAndOpen ().
, ,
-. 8.13
StreamConnectionNotifier.
8.13. StreamConnectionNotifier

StreamConnectionNotifier

StreamConnection
acceptAndOpen ()

182

,
. ,
:
StreamConnection conn =
Connector.open("socket://server.foo.com:98765");
, ;
. , ,
localhost , :
StreamConnection conn =
Connector.open("socket://localhost:98765");
StreamConnectionNotifier.acceptAndOpen(} Connector.open()
StreamConnection. StreamConnection
.
,
StreamConnection ,
. , ,
, . ,
- ,
.
,
StreamConnection.
. - ,
.
, .
, . , ,
, .
,
, , .
,
, .
8.6 - 8.8 ,
MIDP. ,
, .
, .
.
. ,
,

183

. , .
, :

ServerSocket - ,
.
Server Agent - ,
. .
.
ClientSocket - .

8.6. ,
.

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import Java.io.lOException;
/**
,
.
.
,
.
,
. ,
.

. .

.
public class ServerSocket imlements Runnable
{
// ,
// . public static final String DEFAULT_PORT = "9876";
// ,
// . protected String wellKnownPort;
// URI,
// . protected String uri;
// .
protected StreamConnectionNotifier wellKnownConn;
// , ,
protected StreamConnection clientConn;
/**
.
*/
protected ServerSocket()
184

super ();
/**
.
@param port ,
.
*/
public ServerSocket (String port)
}
thisl); if (port == null)
{
wellKnownPort = DEFAULT_PORT;
}
else
}
wellKnownPort = port;
}
setURI(port);
{
protected void setURI(String port)
{
StringBuffer buf = new StringBuffer("socket://:");
buf.append(port); uri = buf.toString();
}
/**
.
.
.
.
*/
public void run()
{
while (true)
{
try
{
//
// . wellKnownConn = (StreamConnectionNotifier)
Connector.open(uri);
// .
// ,
// .
clientConn = wellKnownConn.acceptAndOpen()
// , ,
// .
185

// .
//
// , .
ServerAgent agent = new ServerAgent(clientConn);
Thread thread = new Thread (agent);
} catch (lOException ioe)
( System.out.printlnfioe.getMessage!));
ioe.printStackTrace(); break;
)
}
}
}
8.7. ,
. ,

import javax .microedition. io._StreamConnectior.;
/**
,
.
,
.
.
*/
public class ServerAgent implements Runnable
private StreamConnection conn;
/**
.
@param ,
. ServerSocket
.
*/
public ServerAgent(StreamConnection c)
super (); conn = ;
}
/**
. .
, .
public void run()
}
// . ,
// .
}
}
186

8.8. .
, ,

import javax.microedition.midlet.MI Diet;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.Connector;
import Java.io.lOException;
/**
, .

( DNS) ,
.
*/
public class ClientSocket implements Runnable
{
public static final String P.ROTOCOL = "socket";
/'/ , private String serverPort;
// , , private String serverHostName;
// URI . private String serverURI;
// ..
private StreamConnection streamConn;
protected ClientSocket()
}
super();
}
/**
. DNS
. @param server - DNS ,
.
@param port - , .
*/
public ClientSocket(String server, String port)
throws TOException
(
this();serverHostName = server; serverPort = port;
serverURI = buildServerURI (); open () ;
}
/**
.
@param uri - URI ,
.
@ InvalidArgumentException URI.
*/
public ClientSocket(String uri) throws lOException
{
this (); serverURI = uri;
187

}
. ,
. .
.
@ lOException,
.
*/
public void open() throws lOException
streamConn = (StreamConnection) Connector.open(serverURI);
/**
.
*/
public void closed try streamConn. closed ; }
catch (lOException ioe)
}
ioe.printStackTraced ;
{
{
/**
.
.
, opend .
*/
public void run ()
{
// .
// ,
....
private String buildServerURI ()
}
StringBuffex uri = new StringBuffer(PROTOCOL);
uri.append ("://"); uri.append(serverHostName);
uri.append(":"); uri.append(serverPort); return uri.toString ();
}
}
MIDP. , ,
StreamConnectionNotif ier IOMIDP, ,
, MIDP. , MID-
- .
, , .
.
,
188

.
. , .
.

, ,
. ,
, ,
.

. ,
. -
, - .

,
.
,
. , , ,
.
, .
,
, -
(network address translation (NAT))
. NAT
.
. ,
, -
.
.

. ,
, . ,
,
.
.

. 3G,
.
3G IPv6 .
IPv6 , IP-
.
189

IP-, , ,
.
, , ,
, ,
.

8.9. J2ME J2SE


I MIDP.
java.io MIDP . MIDP java.net, J2SE.
, java.io MIDP - 1
, J2SE.
, BufferedReader, LineNumberReader StringReader java.io J2SE
java.io MIDP.
, ,
MIDP, MIDP
, J2SE. MIDP
:

RMI
;
Jini RMI, ;
JavaSpaces J2ME;
CORBA J2ME.

11, .

, ,
, MIDP
.
, MIDP J2ME.
, CDC . CDC
, CLDC/MIDP. ,
RMI. .
, ,
,
CDC CLDC.
,
, , RMI.
, .

190

8.10.

MIDP javax.microedition.io.

.
MIDP
.
. ,
, .

, . ,
.
Connector, ,
,
.
. ,
, .
,
. ,
.
.
. ,
, HTTP
Unix.
.
MIDP , , RMI, CORBA
Jini. ,
.
, CDC, , , RMI.
MIDP ,
,
.

191

9. ( 9)
9.1.
MIDP .
,
. MIDP,
,
. ,
, .
MIDP .

- Java.
,
, .
,
. ,
, MIDP. ,
MIDP,
. ,
, .
-
, , .
i18n, 18
i n .

9.2.
,
. ,
.
.
11n, 10 l n
.
.
.
, , ,
, .
, - , ,
, .
,
. , , ,
-.
192

, -,
.
,
, , , , , .
, , ,
, -,
. ,
.
, .
,
, ,
(, ).

, .
, ,
, ,
.
- , ,
. ,
,
.
.
,
.
- ,
, , ,
. , , .
118 .
,
.
,
.

9.3. opo
-
, . ,
, .
- ,
- ,
,
.
193

ASCII . ASCII
, - . , Java

. , , ,
. ,
.
,
.
, , .
.
,
. . - ,
, , , ,
.

9.4.Ac
.

, - -
. ,
:

- ( ,
, , )
, .
- ,
, .
- ,
.
-
.
,
, - ,
.


.
.
- , ,
, -
.
. -
,
194

. .
:

;
;

;
.

- ,
.
, .

, .
. , ,
. , ,
- , .
.
,
. , .
, .

. ,
.
. .
, , .
, . ,
, . ,
:
25 decembre 2002
2002/12/25
25/12/2002
08.30
14.45
20.000,45 ( )
, , :
December 25, 2002
12/25/2002
195

8:30 am
2:45 pm
20,000.45 ( )
,
.
,
, .
. , ,
. ,
, ,
.

9.5. MIDP

, . ,
-
-
. MIDP
:

Calendar, Date TimeZone: Java.util;


: microedition.encoding, microedition.locale;
: java.io;
MID-:
( JAD);
() JAR MID-:
Class.getResourceAsStream(String resourceName).

java.util MIDP , , :
Calendar, Date TimeZone. , , .
.
, Calendar , .
Date .
. , , ,
.
Calendar TimeZone . MIDP ,
, . ,
,
MIDP. , , ,
.

196

MIDP .
MIDP
- (GMT).
MIDP .
MIDP
, .
,
, ,
.
, ,
, .

Java,
MIDP. ,
, ,
.

. , ,
.
.
,
. , ,
. , , ,
.
.
, .
, J2SE, , MIDP
(
Java.text J2SE). .
MIDP Java.text. MIDP API
, , ,
,
. MIDP Java.text JDK
1.4, , , , ,
.

9.6.C
,

. J2SE, MIDP API ,
. ResourceBundle
- . MIDP
197

. ,
:

JAD;
,
JAR ;
Java, ,
J2SE.

,
MIDP,
.

9.7.
, MIDP .
J2SE, MIDP API MessageFormat.
MIDP,
:

;
;
;
;
;
, , ,
.;
, .

9.8.C
MIDP .
- ,
. J2SE,
. MIDP .

9.9. ,
MIDP , ,
. MIDP J2SE, :
DateFormat, NumberFormat DecimalFormat.

.
MIDP Date TimeZone java.util,
. - ,
.
198

Date
(UTC). MIDP Date

. J2SE, ,
(, DateFormat), ,
. MIDP .
MIDP java.util.TimeZone.
. MIDP , , ,
GMT. MIDP
GMT, .
TimeZone.getDefault() TimeZone, ,
, , .
, GMT, ,
.
TimeZone.getTimeZone(String id) TimeZone
, . ,
, ,
. , ,
.

9.10.

J2SE MIDP .
java.util.Calendar . MIDP ,
, . ,
.
Calendar, ,
, , .
, ,
. Calendar.getlnstance(TimeZone zone) Calendar,
,
. , Calendar
. ,
. ,
(Chinese Standard Time), , ,
, MIDP. , ,
, ,
.

199

10.

( 10)

10.1.

10

- .
, J2ME Java.
, ,
J2ME, MIDP.
,
, , , ,
.
,
.
,
.
,
.
,
.
.
MIDP
, ,
. ,
, , . ,
, ,
.
,
. , ,

. .
, ,
.

10.2.

,
, .

.
,
. ,
.
200

,
.
,
, .
,
, , ,
.
- - ,
, ,
MIDP (over-the-air (OTA)).

.
, ,
, , ,
, - . ,
, -
.
,
, , .
.
9 ,
(wireless Internet gateway (WIG)).
10.1 ,
.
,
, , 2.5G 3G - ,
, .
,
.
, , ,
.

.
, .
: Wireless Application Protocol (WAP) HTTP-
WAP (SAR) WAP.
, , ,
TCP/IP .
HTML (XHTML) HTTP-, WAP.
2.5G 3G,
.
201

10.1.
,
,
AMS
, , , .

(discovery application (DA)),
MID- . ,
DA
, AMS. Java (Java application
manager (JAM)) AMS ,
Java.
-
,
.
HTTP ,
.
Over The Air User Initiated Provisioning Recommended Practice,
Addendum to the Mobile Information Device Profile , OTA
HTTP. HTTP ,
.
http://java.sun.com/products/midp/. , ,
MIDP-NG ( ).
:

,
,
202

,
,
MIDP .


, ,
, ,
, ,
.
.

.
.
.
, ,
, JAD .
, ,
. ,
OpCos, , ,
.
,
-,
( ). ,
, ,
(,
). ,
,
. ,
. ,
,
.

.

.
.
, ,
.
.

.

. .
203


.
J2ME.
.

10.3.

;
.

, , . ,
, ,
. ,
, :

- ;
- ,
, ,
;
- , ,
, , ,
, .

10.4.

,
. - ,
.
,
. ,
,
.
Web
Web-, HTML.
.

.
JAR, JAD , .
.
URL JAD ( , JAD),
204

,
, .
JAR .

JAR, JAR. ,
, , ,
. - ,

, ,
(service-level agreement (SLA))
.
,
. ,
.
JAD ,
, . ,

. ,
XML, ,
, . , ,
JAD , . ,
,
, .

10.5.

- ,
. ,
.
- WML- ( XHTML)
. ,
, -, , ,
.

.
HTML-, .

,
.
,
, .

. ,
205

.
:

;
;
J2ME;
.


.
, , ,
. .
,
. .
, ,
. , ,
, , . ,
,
. , AMS (JAM)
, , -
.
.
, .
,
.
,
, RMS .
J2ME, , ,
. , , ,
MIDP CLDC. MIDP CLDC, .
,
, .
, .

.
,
. ,
.
,
J2ME.
, ,
, ,
, .
206

HTTP- HTTP-
.
HTTP-, .

.
HTTP-,
.

.
(MSISDN MSN) .
, ,
,
. ,

.
, ,
, . ,
.
. , , WML ( XHTML )
.
( 2G) ( 2.5G
3G), . ^ ,
.
.
, ,
J2ME, ,
.
,
.
, , ,
.
.
. ,
,
,
. ,
, .


.
207

10.6.

-
, J2ME.
.

, .
,
.
,
MicroEdition-Configuration MicroEdition-Profile
JAD. .

10.7.

oy

- ,
, ,
, .
,
, ,
.
.
,
.
,
.

10.8.

-
, , . ,
.
HTTP HTTP- .
HTTP- .
,
.
- MSN , .
,
, , ,
(Lightweight Directory Access Protocol (LDAP))
(WIG)
.
208

10.9.


.
.
.
, ,
, .
. , ,
, , JAD .

10.10.

- .
HTTP-
.

.
. ,
, .
, , , .
.
,
.
,
.
,
. ,
.
(HTTP),
(HTTPS/SSL) .

209

11.

( 11)

11.1.

Web ,
World Wide Web .
- ,
. , ,
, . , ,
.
- -
(wireless Internet gateway (WIG)), ,
,
intranet.
,
(mobile switching center (MSC)).
.
, WIG.

.
11.1 ,
, WIG intranet . WIG
- -
intranet . Intranet
,
.

11.1. WIG
. , WIG
,
,


. , - Web,
. , ,
-.
210


, Web-, ,
, , .
,
,
. .
, -.
, ,
(instant messaging (IM)),
(unified messaging (UM)), ,
. i ,

, .
, -, ,
, ,
-.

.
, Short Message Service (SMS)
.
SMS ,
,
-. ,
,
SMS.
SMS
IM, SMS. , SMS
( )
. , .,
SMS.
IM, ID
. ,
MSN,
ID MSN. ,
, , , , , ,
.
J2ME MIDP IM
. MIDP ICQ IRC
, IM .
, IM (SMS),
SMS .
211

, ,
SMS. SMS 128 .
, 128 . .
,
SMS, 128 .
.
(wireless application protocol (WAP))
. WAP
, WAP,
. WAP
,
(wireless markup language (WML)). , ,
. ,
, HTML WAP,
.
,
, .
. -
, . , , ,

.
-,
, .
, -, ,
, . ,
, WML WAP,
, HTML-.
.
, ,
, . .
,
, ,
.

11.2.

,
.
, ,
.

212

. ,
, ,
, 11.1.
,
.
11.2 ,
, .

, . -
,
.
, 11.2, ,
. , ID, - , ,
.
intranet .
,
.
Web
HTML-, .
J2ME, MIDP , ,
. ,
, Web-.
MIDP -
, -.
M1DP , intranet
.
, MIDP -
,
intranet . MIDP
, 8,
, .
HTTP
. HTTP,
. 11.2 intranet,
.
, , .
,

.
213

TCP/IP
(3G). , , ,
, .

11.2.

, ,
, .
, .
, ,
, ,
, .
.
, ,
J2ME,
. ,
,
.
MIDP .
. WAP,
Web-. ,
, WAP ,
(wireless markup language (WML)). ,

214

, , 3G, XHTML/XML HTTP.


, HTTP.

11.3.

,
.
, , Web-,
. -

, .
, 11.2 , ,
intranet ,
. , ,

, , ,
.
,
.

11.4.

, , :

;
;
.

- ,
. ,
? , .
SMS, SMS
IM. MSN. SMS
, . ,
, , ,
, ,
, .
(e-mail) -
.
, e-mail , :

215

user@some-host.com

. ,
.
SMS, 128 . , ,

.
, SMS.
- ,

.
Web-,
.
, API . ,
MIDP API
SMS .
, ? ,

, , SMS.

11.5.

(Personal information management (PIM))


, , ,
. - .
:
.
,
, , ,
Web-. ,
, Web-,
, -.
(The Internet mail application protocol (IMAP))
(post office protocol (POP))
, .
, .
API ,
Web-, .
HTML--. , ,
, HTTP
API, . SMS
. MIDP, ,
216


. MIDP
. , MIDPNG ( )
MIDP.

11.6.

- ,
.
:

- ,
;
- ,
, , ;
- ,
, , .

.
Web ,
API HTML--.

11.7.

- ,
,
.
,
.
,
Web-.
,
. ,
, .
,
.

. ,

.

. , ,
.
217

,
:

(Global positioning system (GPS)) -


GPS .
-
.
GPS -
.

GPS
GPS . ,
MIDP ,

.
, , , ,
GPS.
. (mobile switching center (MSC))
,
. MSC ,
MSC .
.
GPS GPS ,
GPS intranet
MSC. ,
.
MIDP, MIDP ,
. ,

. ,
, .
, MIDP
, .

11.8.

Apxeypa

- . ,
,
. ,
Carnegie-Mellon University (http://www.sei.cmu.edu):
- ,
, .

218

, ,
, , .
,
UML (UML Modeling Language User Guidz),
:

, , ,
, ,
,
, -
, .
, .
, ,
. (architectural methodology (AM)) - ,
.
,
, . - SunTone AM,
un Microsystems Rational
Unified Process (RUP).
, , ,
, , , SunTone AM,
.
, ,
.
, ,
,
. ,
, J2ME,
, ,
, .
, , J2ME
, .
MIDP , . ,
, MIDP
, ,
, ,
. MIDP MIDP
,
.

219

MIDP
.
, ,
.
- .
,
. , , -

MIDP
.

.

11.9.

- ,
.
, , , ,
, ,
.
SunTone AM , :

- ,
;
-
, ;
-
;
- - ;
- ,
, .


.
,
,
.
, , ,
.

. , , ,
,
, , J2ME, ,
220

.
SunTone AM "Dot-Corn & Beyond".
SunTone AM, , -
.
. ,
. ,
. ,
, , (Alistair Cockburn)
Writing Effective Use Cases,
.
,
. SunTone AM
.
, ,

.
,
.
.
, - .
:
1. -
.
2. - .
3. - , - .
4. -
.
5. - , .
6. - , .
SunTone AM ,
, . , IMAP-
MIDP ,
- .
' .
,
.
.
, ,
,
. ,
, .
221

,
, . ,
.
,
.

.
. ,
MIDP
, , ,
.
, ,
,
. ,
,
.

11.10.

, ,
.
.
:


;

.

,
, , , ,
. ,
.
SunTone AM, ,
.
,
, . ,
, .
SunTone AM - , -
.
,
.

222

,
, ,
, .
,
.
, , ,
,
,
.
:

- , ;
- , , ;
- , ;
- , , .


. MIDP
, , .
SunTone AM
. ,

.
, ,
, .
.
. - ,
.
. , ,
. MIDP,
MIDP
. , ,
,
, .
- ,
, , , . MIDP
, AWT Swing.
MIDP
,
.

223

, , ,
, .
. ,
. - ,
, .
MIDP .

-. ,
MIDP -
.
- , .
,
. , MIDP
.
- , (
). .
, ,
, . ,
, , ,
.

MIDP, MIDP,
. MIDP,
, . , ,
MIDP
, ,
MIDP.
.
. - ,

. MIDP.
MIDP, ,
,
. , , MIDP
.
,
, .
- ,
. ,
MIDP
. - } ,
224

,
.
,
; .
.
MIDP ,

.
.
. ,
. - ,
,
.
.
MIDP , .
. ,
,
-, . AMS
,
.
MIDP, ,
. , .
, ,
- .
, ,
, MIDP. ,
, . ,
MIDP
, , ,
,
.
- , ,
, .

. MIDP, , ,
. ,
,
?
- , .
MIDP.
225

MIDP,
MIDP.
MIDP . -,
MIDP - , , -
, .
-, MIDP , .
, .
.
-
, , .
, MIDP ,
. MIDP,
,
MID.P.
: http://javaformobile.com.ua/samouchitel-j2me.html

226

Оценить