You are on page 1of 38

i h c Khoa h c T nhin HQG-HCM Khoa Cng Ngh Thng Tin

T ng quan v cng ngh J2ME v ki n trc J2ME


ThS. Tr n Minh Tri t

L ch s pht tri n
Vo nh ng nm u c a th p k 90, d a trn ngn ng C++, Sun xy d ng m t ngn ng m i l y tn l Oak nh m pht tri n cc ng d ng dnh cho cc thi t b khng dy v giao ti p c ng h ng ngo i. Sun l c b i cc thnh ph n c a ngn ng C++ nh con tr v vi c qu n l b nh nh m h tr cho cc thi t b gi i h n v b nh v b vi x l. Vo nm 1995, Sun i tn ngn ng Oak thnh Java.

Cc phin b n c a Java
Thng 12/1998, nh m h tr cho cc lo i thi t b c dung l ng b nh v t c b vi x l khc nhau, Sun Microsoft nh ngha cc phin b n chu n khc nhau c a Java h tr cho t ng lo i thi t b c th , g m c 3 phin b n: J2EE (n n t ng Java 2, phin b n doanh nghi p Java 2 Platform, Enterprise Edition): ch y trn my ch l n v i s c m nh x l v dung l ng b nh l n, h tr g n li n v i servlet, jsp v XML. J2SE (phin b n chu n Java 2 Platform, Standard Edition): c dng trn cc my tnh c nhn v laptop. Cc my ny m c d khng m nh hn cc my ch l n song chng v n m nh hn nhi u so v i thi t b di ng. N c th ch y trn cc h i u hnh Linux, Solaris, Microsoft Windows. J2ME (phin b n thu nh - Java 2 Platform, Micro Edition): l phin b n rt g n c a Java dnh cho cc thi t b di ng b gi i h n v b nh v b x l.

J2ME
Chu n J2ME chia cc thi t b lm hai lo i : Thi t b thng tin di ng, c nhn (Personal, Mobile Information devices) c kh nng truy n thng trn m ng m t cch r i r c nh: i n tho i di ng (Mobile phones), my nh n tin hai chi u (two-way pagers), my tr l c nhn dng k thu t s (personal digital asisstants-PDAs). Lo i ny dng cho m t m c ch c bi t, th ng gi i h n v ch c nng, v d a trn c u hnh CLDC ( Connected Limited Device Configuration). Thi t b thng tin k t n i chia s (shared connection Information devices): c k t n i c nh v lin t c nh set-top boxes, Internet TVs, Internet enabled screen phones, high end communicators, h th ng i u h ng xe hi. Lo i ny h tr v giao di n ng i dng v d a trn c u hnh CDC ( Connected Device Configuration).

J2ME
Ta ch xt phin b n CLDC, phin b n J2ME ny dnh cho cc thi t b c b nh gi i h n nh i n tho i di ng. (Ni chung n dng cho cc thi t b di ng ho t ng b ng ngu n pin). Phin b n ny c a Java c n t b nh hn phin b n CDC. J2ME c thi t k ch y trn cc i n tho i di ng c c u hnh t i thi u nh sau: B nh t ng c ng: 128-512 KB B x l: 16 n 32 bit T c x l: 8-32 MHz Nng l ng: gi i h n, ho t ng b ng pin Bng thng: gi i h n, kho ng 9600 bps
6

Ki n trc t ng qut ton b n n t ng Java 2

Ki n trc c a Java 2 v cc thi t b m t ng phin b n h tr

Ki n trc c a J2ME
M c tiu chnh c a J2ME l cho php l p trnh vin vi t cc ng d ng c l p v i thi t b di ng, khng c n quan tm n cc thi t b ph n c ng th t s . V d nh m t l p trnh vin vi t chng trnh game cho i n tho i Samsung th c th s a i chng trnh c a mnh m t cch t i thi u nh t c th ch y trn i n tho i Nokia. t c m c tiu ny, J2ME xy d ng cc t ng (layer) khc nhau che gi u i vi c th c hi n ph n c ng kh i nh pht tri n.

Ki n trc c a J2ME
Ki n trc c a J2ME g m cc t ng chnh l: MIDP (Mobile Information Device Profile) CLDC(Connected limited device configuration), JVM(Java Virtual Machine)

10

Ki n trc c a J2ME
Device Hardware Layer: chnh l cc thi t b di ng th t s v i c u hnh ph n c ng, b nh v t c x l. Khng ph i l m t ph n c a J2ME nhng t ng ny l ni xu t pht, cc thi t b di ng khc nhau c th c b vi x l khc nhau v i cc t p m l nh khc nhau m c tiu c a J2ME l cung c p m t chu n cho t t c cc lo i thi t b di ng khc nhau.

11

Ki n trc c a J2ME
Java Virtual Machine layer: khi m ngu n Java c bin d ch n c chuy n i thnh m bytecode. M bytecode ny sau c chuy n thnh ngn ng my c a thi t b di ng. T ng my o Java bao g m KVM (Kilobyte Virtual Machine) l b bin d ch m bytecode c nhi m v chuy n m bytecode c a chng trnh Java thnh ngn ng my ch y trn cc thi t b di ng. T ng ny cung c p m t s chu n ha cho cc thi t b di ng ng d ng J2ME sau ny khi bin d ch c th ho t ng trn b t k thi t b di ng no c J2ME KVM.

12

Ki n trc c a J2ME
Configuration Layer: nh ngha giao di n ngn ng Java (Java language interface) c b n cho php chng trnh Java ch y trn thi t b di ng. y l m t t p cc API nh ngha li c a ngn ng J2ME. L p trnh vin c th s d ng cc l p v phng th c c a cc API ny tuy nhin t p cc API h u d ng hn c ch a trong t ng profile. Hi n nay Sun a ra 2 d ng Configuration: CLDC - Connected Limited Device Configuration. CDC - Connected Device Configuration .

13

Ki n trc c a J2ME
Profile Layer: m r ng Configuration b ng cch thm vo cc l p h tr cc tnh nng cho t ng thi t b chuyn bi t. M c ch l xy d ng trn l p c u hnh v cung c p nhi u th vi n ng d ng hn, MIDP nh ngha cc API ring bi t cho thi t b di ng. Cng c th c cc hi n tr ng v cc API khc ngoi MIDP c dng cho ng d ng. V d , c th c hi n tr ng PDA nh ngha cc l p v phng th c h u d ng cho vi c t o cc ng d ng PDA (l ch, s h n, s a ch ,). Cng c th c m t hi n tr ng nh ngha cc API cho vi c t o cc ng d ng Bluetooth: Chu n hi n tr ng PDA l c t JSR 75 v chu n bluetooth API l c t JSR - 82 v i JSR l vi t t t c a Java Specification Request

14

My o Java
My o Java (KVM : Kilobyte Virtual Machine) c b t ngu n t h th ng tm ki m c g i l Spotless c pht tri n u tin t i Sun Microsoft Laboratories. H th ng Spotless th c thi h th ng Java cho Palm Connected Organizer. KVM l my o Java linh ho t, g n nh c thi t k cho cc thi t b k t n i m ng c b nh nh , ti nguyn gi i h n. KVM c thi t k v i cc c i m: Yu c u b nh ch kho ng 40-80 kilobytes. Yu c u b nh ng kho ng: 20-40 kilobytes. B vi x l 16/32 bit v i t c ch 25MHz.

15

My o Java
Vai tr c a my o Java (KVM) l d ch m bytecode c sinh ra t chng trnh Java bin d ch sang ngn ng my. Chnh KVM chu n ha output c a cc chng trnh Java cho cc thi t b di ng khc nhau c th coi b vi x l v cc t p l nh khc nhau. Khng c KVM, cc chng trnh Java ph i c bin d ch thnh cc t p l nh cho m i thi t b di ng. Nh v y, l p trnh vin ph i xy d ng nhi u ch cho m i lo i thi t b di ng

16

C u hnh
Hi n nay Sun a ra 2 d ng c u hnh CLDC -C u hnh thi t b k t n i gi i h n CLDC (Connected Limited Device Configuration) c thi t k nh m vo th tr ng cc thi t b c p th p (low-end), cc thi t b ny thng th ng l my i n tho i di ng v PDA v i kho ng 512 KB b nh . V ti nguyn b nh h n ch nn CLDC c g n v i Java khng dy (Java Wireless ), d ng nh cho php ng i s d ng mua v t i v cc ng d ng Java. CDC (Connected Device Configuration) c a ra nh m n cc thi t b c tnh nng m nh hn dng thi t b thu c CLDC nhng v n y u hn cc h th ng my bn s d ng J2SE. Nh ng thi t b ny c nhi u b nh hn (thng th ng l trn 2MB) v c b x l m nh hn. Cc s n ph m ny c th k n nh cc my PDA c p cao, i n tho i web, cc thi t b gia d ng trong gia nh
17

C u hnh
C 2 d ng c u hnh k trn u ch a my o Java (Java Virtual Machine) v t p h p cc l p Java c b n cung c p m t mi tr ng cho cc ng d ng J2ME. Tuy nhin, i v i cc thi t b c p th p, do h n ch v ti nguyn nh b nh v b x l nn khng th yu c u my o h tr t t c cc tnh nng nh v i my o c a J2SE. V d , cc thi t b thu c CLDC khng c ph n c ng yu c u cc php tnh ton d u ph y ng, nn my o thu c CLDC khng c yu c u h tr ki u float v double. Ngha l, th vi n CLDC khng h tr l p java.lang.Float, phng th c Object.finalize( ) cng khng c h tr .

18

M i quan h gi a CLDC v CDC

19

So snh gi a CLDC v CDC

CLDC

CDC

Ram Rom

>=32K, <=512K >=128k, <=512k

>=256K >=512k

Ngu n nng l ng

C gi i h n (ngu n pin)

Khng gi i h n

Network

Ch m

Nhanh

20

Profile
Profile m r ng Configuration b ng cch thm vo cc class b tr cc tnh nng cho t ng thi t b chuyn bi t. C 2 Configuration u c nh ng profile lin quan v t nh ng profile ny c th dng cc class l n nhau. n y ta c th nh n th y do m i profile nh ngha m t t p h p cc class khc nhau, nn th ng ta khng th chuy n m t ng d ng Java vi t cho m t profile ny v ch y trn m t my h tr m t profile khc. Cng v i l do , b n khng th l y m t ng d ng vi t trn J2SE hay J2EE v ch y trn cc my h tr J2ME

21

M t s Profile tiu bi u
MIDP (Mobile Information Device Profile): b sung cc tnh nng nh h tr k t n i, cc thnh ph n h tr giao di n ng i dng vo CLDC. Profile ny c thi t k ch y u nh m vo i n tho i di ng v i c tnh l mn hnh hi n th h n ch , dung l ng ch a c h n. Do MIDP s cung c p m t giao di n ng i dng n gi n v cc tnh nng m ng n gi n d a trn HTTP. C th ni MIDP l profile n i ti ng nh t b i v n l ki n th c c b n cho l p trnh Java trn cc my di ng. PDA Profile: tng t MIDP, nhng v i th tr ng l cc my PDA v i mn hnh v b nh l n hn. Foundation Profile: cho php m r ng cc tnh nng c a CDC v i ph n l n cc th vi n c a b Core Java2 1.3 Ngoi ra cn c Personal Basis Profile, Personal Profile, RMI Profile, Game Profile.

22

M t s Profile tiu bi u

23

MIDP
y l Profile c nh ngha dnh ring cho cc thi t b di ng v l thnh ph n chnh trong J2ME Nh ng ch c nng MIDP cung c p:
Cc l p v ki u d li u: ph n l n cc l p m cc l p trnh vin Java quen thu c v n cn c gi l i v d nh cc l p trong gi java.util nh Stack, Vector v Hastable cng nh Enumeration. H tr i t ng Display: ng nh tn g i m t chng trnh MIDP s h tr duy nh t m t i t ng Display l i t ng qu n l vi c hi n th d li u trn mn hnh i n tho i. H tr Form v cc giao di n ng i dng. H tr Timer v Alert Cung c p tnh nng Record Management System (RMS) cho vi c lu tr d li u.

24

MIDP
Nh ng ch c nng MIDP khng th lm c: Php tnh d u ch m ng (floating point): php tnh ny i h i r t nhi u ti nguyn CPU v ph n l n cc CPU cho cc thi t b di ng khng h tr php tnh ny, do MIDP cng khng c. B n p class (Class Loader). H tr t kha finalize( ) nh trong J2SE: vi c d n d p ti nguyn tr c khi n b xa do l p trnh vin th c hi n. Khng h tr JNI (Java Native Interface). H tr h n ch thao tc b t l i. Ph n l n cc th vi n API cho Swing v AWT khng th s d ng c trong MIDP. Khng h tr cc tnh nng qu n l t p tin v th m c, trong th c t l cc thi t b J2ME khng c h tr cc thi t b lu tr thng th ng nh c ng. Tuy nhin, i u khng c ngha l ph i m t i m i d li u quan tr ng m i khi t t my, Sun cung c p m t ch c nng khc tng ng g i l Record Management System (RMS) cung c p kh nng lu tr cho cc thi t b ny.
25

MIDP 2.0
Thng 11 nm 2003 Sun tung ra MIDP 2.0 v i hng lo t tnh nng khc c cung c p thm so v i phin b n 1.0. M t s cc c i ti n n i b t so v i MIDP 1.0: Nng c p cc tnh nng b o m t nh: Download qua m ng an ton hn thng qua vi c h tr giao th c HTTPS. Ki m sot vi c k t n i gi a my di ng v server, v d cc chng trnh khng th k t n i t i server n u khng c s ch p nh n c a ng i dng. Thm cc API h tr Multimedia. M t trong nh ng c i ti n n i b t nh t c a MIDP 2.0 l t p cc API media c a n. Cc API ny l m t t p con ch h tr m thanh c a Mobile Media API (MMAPI). M r ng cc tnh nng c a Form. Nhi u c i ti n c a vo API javax.microedition.lcdui trong MIDP 2.0, nhng cc thay i l n nh t (ngoi API cho game) l trong Form v Item.
26

MIDP 2.0
M t s cc c i ti n n i b t so v i MIDP 1.0: H tr cc l p trnh vin game b ng cch tung ra Game API. c h ng l i nh t t Game API trong MIDP 2.0 khng ch l cc l p trnh vin game m cn cc l p trnh vin c n s d ng cc tnh nng h a cao c p. H tr ki u nh RGB: m t trong nh ng c i ti n h p d n cho cc nh pht tri n MIDP l vi c bi u di n hnh nh d i d ng cc m ng s nguyn, cho php MIDlet thao tc v i d li u hnh nh m t cch tr c ti p.

27

Ti n trnh xy d ng MIDlet

28

Qu trnh pht tri n ng d ng MIDlet v i IDE


L p trnh vin : t o ra cc t p tin ngu n Java (*.java). Trn IDE: B bin d ch Java (Java Compiler): bin d ch m ngu n java thnh bytecode. M bytecode ny s c KVM d ch thnh m my. M bytecode bin d ch c lu trong cc t p tin *.class v s c t p tin *.class sinh ra cho m i l p java. B ti n ki m tra (Preverifier): ki m tra tnh h p l c a m bytecode. M t trong nh ng yu c u an ton c a J2ME l b o m m bytecode chuy n cho KVM l h p l v khng truy xu t cc l p hay b nh ngoi gi i h n c a chng. Do , t t c cc l p u ph i c ti n ki m tra tr c khi chng c th a ln thi t b di ng.
29

Qu trnh pht tri n ng d ng MIDlet v i IDE


Trn IDE: T o t p tin JAR: IDE s t o m t t p tin JAR ch a cc thng tin sau:
T t c cc t p tin *.class. Cc hnh nh c a ng d ng. Cc t p tin d li u c th c yu c u b i ng d ng. M t t p tin k khai (manifest.mf) cung c p m t v ng d ng cho b qu n l trn thi t b di ng. T p tin JAR c bn ho c c phn ph i n ng i dng.

30

Qu trnh pht tri n ng d ng MIDlet v i IDE


Ng i dng: download ng d ng v thi t b di ng. Trn thi t b di ng: tr c khi ng d ng c download v thi t b di ng, b ti n ki m tra s ki m tra m bytecode c h p l hay khng; n u h p l , b qu n l ng d ng s lu chng trnh vo thi t b di ng. KVM: Khi ng i dng th c thi ng d ng, b qu n l ng d ng s chuy n ng d ng cho KVM ch y trn thi t b di ng. KVM s d ch m bytecode sang ngn ng my c a thi t b di ng ch y.

31

Mi tr ng pht tri n J2ME


J2ME Wireless Toolkit http://java.sun.com/j2me/download.html JDK 1.5 ho c m i hn http://java.sun.com/j2se/1.5/download.html

32

J2ME Wireless Toolkit


Sau khi ci t, th c thi chng trnh:

33

J2ME Wireless Toolkit


Giao di n c a chng trnh

34

J2ME Wireless Toolkit


New Project, nh p vo tn Project v Class:

Khng nh t thi t ph i t o trng tn class v i tn Project. Trn c a s chng trnh s hi n th cc thng bo v vi c t o cc th m c ph c v cho vi c xy d ng v th c thi m chng trnh
35

V d n gi n
import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class TestMidlet extends MIDlet implements CommandListener { private Form mMainForm; public TestMidlet() { mMainForm = new Form("Lap trinh voi J2ME"); mMainForm.append( new StringItem(null, "Hello world!, MIDP!")); mMainForm.addCommand( new Command("Exit", Command.EXIT, 0)); mMainForm.setCommandListener(this); }

36

V d n gi n (tt)
public void startApp() { Display.getDisplay(this).setCurrent(mMainForm); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) { notifyDestroyed(); } }

37

J2ME Wireless Toolkit


Copy source ny vo th m c ch a m ngu n c a project. Build v run chng trnh

38