Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thái Duy Quý - Khoa CNTT-Đại học Đà Lạt

Similar presentations


Presentation on theme: "Thái Duy Quý - Khoa CNTT-Đại học Đà Lạt"— Presentation transcript:

1 Thái Duy Quý - quytd@dlu.edu.vn Khoa CNTT-Đại học Đà Lạt
PHẦN II: CĂN BẢN VỀ J2ME Thái Duy Quý - Khoa CNTT-Đại học Đà Lạt

2 Nội dung J2ME là gì? Các khái niệm chính của J2ME
Configurations (CLDC, CDC) Profiles (MIDP) MIDlet, MIDlet Suite Các lớp API cơ bản Cài đặt và sử dụng bộ Toolkit Lập trình J2ME trên Eclipse

3 Phiên bản Java Java gồm có 3 phiên bản.
Mỗi phiên bản cung cấp một mội trường hoàn chỉnh cho việc chạy các ứng dụng Java, bao gồm: Máy ảo Java (JVM – Java Virtual Machine) Các lớp thời gian chạy. 3 phiên bản này nhằm tới các ứng dụng khác nhau chạy trên các loại thiết bị khác nhau.

4 workstation applications
Phiên bản Java (2) Java2 Standard Edition (J2SE™) Enterprise Edition (J2EE™) Micro Edition (J2ME™) Java 2 Platform Standard desktop & workstation applications Heavy duty server systems Small & memory constrained devices

5 Phiên bản Java (3) Mỗi phiên bản định nghĩa tập các thư viện lớp khác nhau. Có đến hàng ngàn lớp thời gian chạy J2SE chính, khoảng MB. J2ME- có số lớp ít hơn. J2ME J2SE J2EE

6 Kiến trúc J2ME Configuration
Profile Libraries Java Virtual Machine Profiles Configuration Host Operating System Java Language Configuration Yêu cầu nền tối thiểu cho một nhóm thiết bị. Profile Đặc tả cần thiết cho một họ thiết bị

7 Các khái niệm J2ME Configurations Profiles Optional packages

8 Configurations Configuration là một môi trường thời gian chạy Java hoàn chỉnh, bao gồm: Java virtual machine (VM): thực thi Java bytecode Mã tự nhiên để giao tiếp với hệ thống cơ sở Tập các lớp thời gian chạy Java chính. Để sử dụng một configuration, một thiết bị phải đạt được các yêu cầu tối thiểu.

9 Connected Limited Device Configuration Connected Device Configuration
Configurations Connected Limited Device Configuration Connected Device Configuration

10 So sánh CLDC & CDC Tổng bộ nhớ:160 - 512 KB
Bộ xử lý:16-bit hoặc 32-bit Tiêu thụ năng lượng thấp (dùng pin) Kết nối với băng thông giới hạn CDC Tổng bộ nhớ: 2MB hoặc hơn đối với nền(flatform) Java Bộ xử lý: 32-bit Kết nối với băng thông mạng cao, thường sử dụng TCP/IP

11 KVM & CVM KVM – máy ảo Java dành cho cấu hình CLDC
CVM - máy ảo Java dành cho cấu hình CDC Many device manufacturers license the KVM or CVM from Sun Microsystems to serve as the core of their J2ME implementation, they are not required for J2ME compliance.

12 CLDC & CDC – Tập con J2SE CLDC bao gồm các lớp từ:
java.lang java.io java.util Chỉ bao gồm các lớp được chọn từ mỗi package CDC bao gồm 17 package Bao gồm cả các lớp trong package chia sẻ

13 CLDC & CDC – Tập con J2SE (2)

14 Các khái niệm chính J2ME (2)
Configurations Profiles Optional packages By itself, the CLDC is a limited programming platform. Because it does not define any user interface classes or implement any I/O models, about all you can do for output is write to the System.out stream, which may or may not be captured to a console or file. You really need the extra classes defined by a J2ME profile (like those of the MIDP) or device-specific classes (like those on the RIM BlackBerry devices or certain Japanese i-Mode phones) to do anything interactive.

15 Profiles Thêm các lớp đặc tả miền cho một configuration:
Để lắp đầy các chức năng còn thiếu Để hỗ trợ sử dụng đặc tả của một thiết bị Hầu hết các profiles định nghĩa các lớp giao diện người dùng cho việc xây dựng các ứng dụng tích hợp. Để sử dụng profile, thiết bị phải đáp ứng yêu cầu tối thiểu của configuration và profile cơ sở.

16 Profiles (2) Mobile Information Device Profile
Personal Digital Assistant Profile Foundation Profile Personal Basis Profile Personal Profile

17 MIDP – MID Profile MIDP là profile dành cho các MID - Mobile Information Device Đặc trưng tối thiểu của MID: Đủ bộ nhớ để chạy các ứng dụng MIDP Độ phân giải tối thiểu 96 X 56 pixels, màu hoặc trắng đen Bộ phím: keypad, keyboard, hoặc touch screen Khả năng mạng không dây 2 chiều Almost any wireless device built these days fits the definition of a MID, including low-end cellphones. Personal digital assistants (PDAs) can also be considered to be MIDs because wireless networking is now an option for most PDAs, but MIDP doesn't target these devices. Personal digital assistants (PDAs) can also be considered to be MIDs because wireless networking is now an option for most PDAs, but MIDP doesn't target these devices specifically: PDAs have more memory, larger screens, and interesting information management capabilities that are more effectively exploited using other profiles.

18 MIDP – Đặc tả Có 2 phiên bản MIDP:
MIDP 1.0 – 9/2000: Nhiều thiết bị trên thì trường hiện tại hỗ trợ MIDP 2.0 – 11/2003: Nhiều cải tiến vượt bậc so với MIP 1.0 Nâng cấp tính năng bảo mật: HTTPS, kết nối tới server Thêm các API hỗ trợ Multimedia Mở rộng các tính năng của Form Hỗ trợ Game API Hỗ trợ kiểu ành RGB The MIDP 1.0 specification was defined by an expert group consisting of all the major players in the wireless and handheld device arena, including familiar names like Motorola, Nokia, Ericsson, Research in Motion, and Symbian. It has a lot of support in the telecommunications industry, and handset manufacturers like Motorola and Nokia in particular are devoting a lot of development effort to supporting MIDP in a wide range of their devices.

19 CÁC KHÁI NIỆM CHÍNH J2ME (3)
Configurations Profiles Optional packages

20 Optional Packages Tập các API hỗ trợ thêm
Độc lập với một cấu hình và một hoặc nhiều profile. Các optional package: RMI Bluetooth JDBC Have minimum requirements. RMI – Remote Method Invocation JDBC – Java Data Base Connectivity

21 Chọn ứng dụng J2ME Ứng dụng J2ME là một dạng không rõ ràng (ambiguous term). Nên chọn cụ thể Configuration, profile và các optional package Các CDC-based profile giúp phát triển đơn giản hơn J2SE, nhưng không phù hợp với các low-end device Các CLDC-based profile khó hơn cho việc phát triển, đặc biệt là cố gắng hạn chế kích thước ứng dụng để chạy trên nhiều thiết thị nhỏ

22 MIDlet Phần quan trọng nhất của J2ME
MIDP không dùng phương thức static main, cũng không gọi phương thức System.exit để thoát ứng dụng Thay vậy, chúng ta sử dụng MIDlet, một ứng dụng MID Profile The first concept we have to understand is the MIDlet

23 MIDlet (2) Mỗi ứng dụng phải kế thừa lớp javax.microedition.midlet.MIDlet để cho phép phần mềm quản lý ứng dụng: Quản lý MIDlet Có thể lấy các đặc trưng từ bộ mô tả ứng dụng Thông báo và yêu cầu các thay đổi trạng thái Extends MIDlet like extending Applet class

24 MIDlet (3) Lớp kế thừa là lớp chính của ứng dụng
Lớp MIDlet định nghĩa các phương thức trừu tượng mà lớp chính thực thi như: startApp(), destroyApp(), notifyDestroyed()

25 MIDlet Suite (4) Một hay nhiều MIDlet được đóng gói vào một MIDlet suite, bao gồm: JAR (Java archive) file JAD (Java Application Descriptor) file Tất cả các lớp người dùng định nghĩa và tài nguyên được cần thiết cho MIDlet Suite nằm trong file JAR A Java Application Descriptor (JAD) file. This file contains a predefined set of attributes that allows the device application management software to identify, retrieve, and install the MIDlets. A Java Archive (JAR) file. The JAR file contains Java classes for each MIDlet in the suite and Java classes that are shared between MIDlets. The JAR file also contains resource files used by the MIDlets and a manifest file.

26 MIDlet Suite (5) File JAR cũng phải chứa file manifest mô tả các MIDlet trong Suite. File JAD chứa thông tin tương tự, và được sử dụng bởi thiết bị đế lấy thông tin vềmột MIDlet Suite mà không phải download và cài đặt MIDlet Suite Show manifest and jad files

27 Creating a MIDlet

28 Khai báo các gói đặc tả MIDP
Creating a MIDlet Khai báo các gói đặc tả MIDP import javax.microedition.lcdui.*; import javax.microedition.midlet.*;

29 Thực thi giao diện CommandListener
Creating a MIDlet Kế thừa MIDlet Thực thi giao diện CommandListener public class HelloMIDlet extends MIDlet implements CommandListener {

30 Tạo form, thêm các Command
Creating a MIDlet Tạo form, thêm các Command public HelloMIDlet() { mMainForm = new Form("HelloMIDlet"); mMainForm.append(new StringItem(null, "Hello, Seminar Software Design!")); mMainForm.addCommand(new Command("Exit", Command.EXIT, 0)); mMainForm.setCommandListener(this); } Show the DEMO – HelloSuite.

31 Các lớp API Mẫu MIDlet Form Command Item CommandListener

32 Các lớp API Mẫu MIDlet – Lớp cơ sở Form Command Item
CommandListener ItemCommandListener Item

33 Lớp MIDlet protected abstract void startApp()
protected abstract void pauseApp() protected abstract void destroyApp(boolean unconditional) public final String getAppProperty(String key) public final void notifyDestroyed()

34 Các lớp API mẫu (2) Form Command Item MIDlet CommandListener
ItemCommandListener Item

35 Lớp Form Form là màn hình chứa các thành phần như: image, text field, date field, gauge, choice group, và custom item. Tổng quát, bất kỳ lớp con nào của lớp Item có thể chứa trong form Layout, traversal, và scrolling.

36 Lớp Form (2) Quản lý Item Layout public Item get(int itemNum)
public int append(Item item) public Item get(int itemNum) Layout public void setItemStateListener(ItemStateListener iListener) The items contained within a Form may be edited using append, delete, insert, and set methods. Items within a Form are referred to by their indexes An item may be placed within at most one Form Items Layout - Layout policy in Form is organized around rows . Forms grow vertically and scroll vertically as necessary. The height of a Form varies depending upon the number of rows and the height of each row. An implementation may choose to lay out Items in a left-to-right or right-to-left direction depending upon the language conventions in use. You can use LAYOUT_LEFT, LAYOUT_RIGHT,LAYOUT_CENTER, considering row-breaks etc.

37 Các lớp API mẫu (3) Command MIDlet Form Item CommandListener
ItemCommadListener Item

38 Lớp Command Lớp Command biểu diễn mặc ngữ nghĩa của một hành động Hành động được thực thi trong đối tượng CommandListener CommandListener được kết hợp với một Displayable hoặc Item Một khi Command được gọi –CommandListener được gọi, và hành động đucợ thực thi. Displayable: (an object that has the capability of being placed on the display(Screen,Canvas)

39 Lớp Command (2) Command label public String getLabel() Command type
public int getCommandType() Command priority public int getPriority() Possible Command types: The defined types are BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN, and STOP.

40 Lớp CommandListener Đối tượng này là bộ lắng nghe các Command được đưc vào một Displayable public void commandAction(Command c, Displayable d) CommandListener – attaching the listener to the entire screen, the command can be of type SCREEN or anything else…

41 Lớp ItemCommandListener
Khi một Command (được đưa vào một Item) được gọi, ứng dụng được thông báo có commandAction() được gọi trong ItemCommandListener public void commandAction(Command c, Item item) ItemCommandListener attaching the listener to a specific item, the command should be of type ITEM. A listener type for receiving notification of commands that have been invoked on Item objects.

42 Các lớp API mẫu (4) Item MIDlet Form Command CommandListener
ItemCommandListener Item

43 Lớp Item Lớp cha của các thành phần có thể được thêm vào Form. Tất cả các đối tượng Item có một trường label Layout, size, và appearance Thêm vào các Command Item has many layouts, including LAYOUT_DEFAULT , LAYOUT_LEFT , LAYOUT_RIGHT , LAYOUT_CENTER , LAYOUT_TOP , LAYOUT_BOTTOM , LAYOUT_VCENTER , LAYOUT_NEWLINE_BEFORE , LAYOUT_NEWLINE_AFTER , LAYOUT_SHRINK, LAYOUT_VSHRINK , LAYOUT_EXPAND , LAYOUT_VEXPAND , LAYOUT_2 Sizes: the implementor can set the sizes of an item. If the application attempts to lock a preferred size dimension to a value smaller than the minimum or larger than the maximum, the implementation may disregard the requested value and instead use either the minimum or maximum as appropriate. If this occurs, the actual values used must be visible to the application via the values returned from the getPreferredWidth and getPreferredHeight methods. Appearance: The StringItem and ImageItem classes have an appearance mode attribute that can be set in their constructors. This attribute can have one of the values PLAIN, HYPERLINK, or BUTTON

44 Lớp Item (2) public void setDefaultCommand(Command cmd)
public void setItemCommandListener(ItemCommandListener listener) public void notifyStateChanged() public int getPreferredWidth()

45 Cài đặt và sử dụng bộ Toolkit
Bước 1: Download sun’s J2ME Wireless Toolkit từ: Bước 2: Kiểm tra J2SE SDK đã được cài đặt Bước 3: Cài đặt J2ME Toolkit. There’s sun’s download center Choose the relevant operating system. Available for windows, linux, solaris. (not for OS X, although MIDP is available). You need java sdk 1.3 or higher Install the toolkit in a directory with no space in its name…

46 Sau khi cài đặt

47 Sử dụng KToolbar KToolbar is the main working tool to handle J2ME applications You can create a new project, open an existing one, choose the emulator device, change the settings, build (compile and preverify) and run the simulation.

48 Tạo một ứng dụng Chọn “File\New Project”
Nhập tên project và lớp MIDlet Một thư mục mới chứa project sẽ tự động được tạo.

49 Thư mục được tạo ở đâu?
J2ME/apps/{proj} source, resource, and binary files J2ME/apps/{proj}/bin JAR, JAD, unpacked manifest files. J2ME/apps/{proj}/lib external class libraries, (JAR or ZIP) for a specific project J2ME/apps/{proj}/res resource files J2ME/apps/{proj}/src source files J2ME/apps/lib external class libraries, (JAR or ZIP) for all KToolbar projects.

50 Và sau đó… Chọn platform Viết code Save
Build (Compile + Preverify) Run

51 Lập trình J2ME trên Eclipse
Thiết kế theo mô hình platform Kiến trúc mở cho phép gắn các plugin để sử dụng các công cụ cần thiết EclipseMe Plugin dành riêng cho phát triển ứng dụng J2ME Thừa hưởng đầy đủ các tính năng của Java IDE từ Eclipse.

52 Cài đặt EclipseMe Bước 1: Download tại địa chỉ
Bước 2: Kiểm tra yêu cầu Để chạy được EclipseME, cần phải có J2ME Wireless Toolkit (WTK) 1.0, JDK 1.4 và Eclipse 3.0M9 trở lên. Bước 3: Cài đặt EclipseMe Để cài đặt EclipseMe, chỉ cần giải nén file zip đến thư mục plugins của Eclipse

53 Cài đặt EclipseMe (2) Bước 4: Khởi động Eclipse
Menu Window -> Preferences. Mở rộng mục J2ME, chọn mục Platform Components. Trong khung bên phải, kích chuột phải vào mục Wireless Toolkits, kích vào Add Wireless Toolkit. Cửa sổ hiện ra yêu cầu bạn chọn thư mục cài đặt WTK. Nhấn nút Browse và chọn thư mục WTK đã cài đặt. Nhất Finish rồi nhấn OK. Quá trình cài đặt và cấu hình đã hoàn tất.

54 Cài đặt EclipseMe (3)

55 Tạo Project J2ME Menu File -> New -> Project
Mở rộng mục J2ME, chọn J2ME MIDlet Suite. Nhấn Next. Gõ tên project (ví dụ HelloWorld) và chọn thư mục chứa project (ví dụ C:\projects\J2ME). Nhấn Next. Chọn nền WTK. Nhấn Next. Có thể thêm các thư viện cần dùng, thêm thư mục chứa source v.v… Cũng có thể để mọi thứ theo mặc định. Nhấn Finish để kết thúc quá trình tạo J2ME project.

56 Tạo MIDlet Trong khung Navigator, kích chuột phải trên J2ME project (theo ví dụ ở trên là HelloWorld). Chọn New -> Other. Mở rộng mục J2ME, chọn J2ME Midlet. Nhấn Next. Nhập tên gói chứa MIDlet trong mục package, ví dụ: edu.eclipseme.helloworld Nhập tên lớp MIDlet trong mục Name, ví dụ: HelloWorldMIDlet. Nhấn Finish để kết thúc quá trình tạo MIDlet

57 Chương trình HelloWorldMIDlet
Chương trình sẽ vẽ một dòng chữ Hello World in đậm màu đỏ ở giữa màn hình điện thoại. Trước khi in dòng chữ, chương trình hiển thị một màn hình intro trong 30 giây với một hình ảnh dạng png làm logo.

58 Lớp HelloCanvas package eclipseme.HelloWorld; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; public class HelloCanvas extends Canvas { protected void paint(Graphics g) clearScreen(g); paintHello(g); } private void clearScreen(Graphics g) g.setColor(0xFFFFFF); g.fillRect(0, 0, getWidth(), getHeight()); private void paintHello(Graphics g) { String hello = "Hello World"; g.setColor(0xFF0000); Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM); g.setFont(font); int x = (getWidth() - font.stringWidth(hello)) / 2; int y = (getHeight() - font.getHeight()) / 2; g.drawString(hello, x, y, Graphics.TOP | Graphics.LEFT); }

59 Lớp IntroCanvas package eclipseme.HelloWorld; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; public class IntroCanvas extends Canvas{ private static final String logoName = "/Images/image.png"; private Display display; private Displayable nextScreen; private int timeout; private Timer timer = new Timer(); public IntroCanvas(Display display, Displayable nextScreen, int timeout){ this.display = display; this.nextScreen = nextScreen; this.timeout = timeout; } public void startIntro(){ display.setCurrent(this); public void endIntro(){ display.setCurrent(nextScreen);

60 Lớp IntroCanvas (2) protected void paint(Graphics g) { try { Image logo = Image.createImage(logoName); g.drawImage(logo, 0, 0, Graphics.TOP | Graphics.LEFT); } catch (IOException e) { g.drawString("Logo Error", 0, 0, Graphics.TOP | Graphics.LEFT); protected void showNotify() { timer.schedule(new CountDown(), timeout); } protected void keyPressed(int keyCode) { endIntro();} private class CountDown extends TimerTask { public void run() {endIntro();}

61 Lớp HelloWorldMIDlet
package eclipseme.HelloWorld; import javax.microedition.lcdui.Display; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class HelloWorldMIDlet extends MIDlet { private Display display; private IntroCanvas introCanvas; private HelloCanvas helloCanvas; public HelloWorldMIDlet() { super(); } protected void startApp() throws MIDletStateChangeException { display = Display.getDisplay(this); helloCanvas = new HelloCanvas(); introCanvas = new IntroCanvas (display, helloCanvas, 3000); introCanvas.startIntro(); } protected void pauseApp() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}

62 Kết quả

63 The end!


Download ppt "Thái Duy Quý - Khoa CNTT-Đại học Đà Lạt"

Similar presentations


Ads by Google