SlideShare une entreprise Scribd logo
1  sur  19
JAVA ME LWUIT, STORAGE & CONNECTIONS Fafadia-Tech PrasanjitDey prasanjit@fafadia-tech.com
LWUIT The Lightweight User Interface Toolkit (LWUIT) is a versatile and compact API for creating attractive application user interfaces for mobile devices LWUIT provides sophisticated Swing-like capabilities without the tremendous power and complexity of Swing LWUIT offers a basic set of components, flexible layouts, style and theming, animated screen transitions, and a simple and useful event-handling mechanism LWUIT is developed by Sun Microsystems and is inspired by Swing
Key Features Layouts Manager Pluggable Look and Feel & Themes Fonts Touch Screen Animations & Transitions 3D and SVG Graphics Integration Tools Bi-directional text support
Simple Lwuit Program import javax.microedition.midlet.*; import com.sun.lwuit.*; // imports LWUIT import com.sun.lwuit.layouts.BorderLayout; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; import java.io.IOException; public class Hello extends MIDlet implements ActionListener { 	public void startApp() { Display.init(this); // initializes the display       try { 	// using a theme here             Resources r = Resources.open("/res/ThemeJava1.res"); UIManager.getInstance().setThemeProps(r.getTheme("ThemeJava1")); 	}      catch (IOExceptionioe) {     	  // Do something here.      }
Continued 	Form f = new Form("Hello, LWUIT!"); f.addComponent("Center",new Label("Prasanjit's j2me 	apps ")); f.show(); // display the form     Command exitCommand = new Command("Exit"); f.addCommand(exitCommand); f.setCommandListener(this);     } 	public void pauseApp() { } 	public void destroyApp(boolean unconditional) {} 	public void actionPerformed(ActionEventae) { notifyDestroyed();     } }
Output Download the LWUIT zip from http://sun.java.com/javame/technology/lwuit and add it to your project /resources directory, also add all your themes, images and other resources in a folder, zip it and add to your project/resources directory.
Simple Layout Example import javax.microedition.midlet.*; import com.sun.lwuit.layouts.BoxLayout; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; import com.sun.lwuit.events.ActionListener; import com.sun.lwuit.events.ActionEvent; import java.io.IOException; public class Layouts extends MIDlet implements ActionListener { 	Form form;     	Command exit;     	public void startApp() { Display.init(this);     	try {     		Resources r = Resources.open("/res1/ThemeJava2.res"); UIManager.getInstance().setThemeProps(r.getTheme("ThemeJava2")); 	} catch(IOExceptionioe) { System.out.println(ioe);     } 	form = new Form("Layouts ");
Continued 	// adding 5 buttons on the form along with a command 	Container buttonBar = new Container(new BoxLayout(BoxLayout.X_AXIS)); buttonBar.addComponent(new Button("Add")); buttonBar.addComponent(new Button("Remove")); buttonBar.addComponent(new Button("Edit")); buttonBar.addComponent(new Button("Send")); buttonBar.addComponent(new Button("Exit"));     	exit = new Command("Exit"); form.addComponent(buttonBar); // buttonBar is a container for other buttons form.addCommand(exit); form.setCommandListener(this); form.show();  	public void pauseApp() {}     	public void destroyApp(boolean unconditional) {}     	public void actionPerformed(ActionEventae) { notifyDestroyed();     } }
Simple Event Handling import javax.microedition.midlet.*; import java.io.IOException; import com.sun.lwuit.*; import com.sun.lwuit.events.*; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; public class EventHandling extends MIDlet implements ActionListener {     Form form; int c = 1000;     Label l1;     Button b1,b2;     public void startApp() { Display.init(this); 	    try {        	Resources r = Resources.open("/res1/ThemeJava2.res"); UIManager.getInstance().setThemeProps(r.getTheme("ThemeJava2")); 	} catch(IOExceptionioe) { System.out.println(ioe);     }
Continued 	Form = new Form("Event handling");     	l1 = new Label(" ");     	b1 = new Button("Change label to add "); form.addComponent(b1); form.addComponent(l1); form.show();     	b1.addActionListener(this); }     public void pauseApp() {     }     public void destroyApp(boolean unconditional) {     }     public void actionPerformed(ActionEventae)     { c++;             l1.setText("" +c); form.layoutContainer();     } }
Storage Record Management System or rms is used to provide the storage capabilities in Java ME It stores records in binary format inside the Record Stores Data is not lost even if the device is switched off rms provides various methods for storing and retrieving records: openRecordStore() closeRecordStore() deleteRecordStore() getRecord() enumerateRecords()
rms Example import java.io.*; import javax.microedition.midlet.*; import javax.microedition.rms.*; // imports all rms resources public class rmsDemo extends MIDlet { intsize_available, id; RecordStorers; 	public rmsDemo() { openStore(); // open recordstore addRecord(); // adds record into recordstore getRecord(); // gets all rcords closeStore(); // closes the recordstore 	} 	public void startApp() {} 	public void pauseApp() {} 	public void destroyApp(boolean unconditional) {} 	public void openStore() { 	try { rs=RecordStore.openRecordStore("names",true); size_available=rs.getSizeAvailable(); System.out.println("Start"); System.out.println("Available size is " +size_available); 	}
Continued 	catch(Exception e) {	 System.out.println(e); 	} 	} 	public void closeStore() { 	try { rs.closeRecordStore(); 	} catch(Exception e) {	 System.out.println(e); 	} 	} 	public void addRecord() { 	try { 		String record="java ME persistent storage"; 		byte data[]=record.getBytes(); // converts into bytes of data 		id=rs.addRecord(data,0,data.length); 	} catch(Exception e) {	 System.out.println(e); 	} }
Continued 	public void getRecord() { 	try { 		byte getData[]= rs.getRecord(id); // gets the record System.out.print("Records in byte format: "); 		for(int j=0;j<getData.length;j++) 		{ System.out.print(getData[j]); System.out.print(" "); 		} System.out.println(); System.out.print("Records in string format: "); 		for(inti=0;i<getData.length;i++) 		{ System.out.print(new String(new byte[]{getData[i]})); 		} System.out.println(); System.out.println("Done with it"); 	} catch(Exception e) {	 System.out.println(e); 	}  	} }
Connections All the important classes and methods for connecting the to the wireless network are included in the javax.microedition.io.* package The connections types are provided by the InputStream and the OutputStream interfaces These interfaces adds the ability to input and output data over the network There are three important level of connections available  Socket Datagram HTTP connection
Http Example In this program, we create a form and add a command to it. On clicking the command, the data from the url is displayed on the device. Here is the actual thread to display the data on the device. public void commandAction(Command c, Displayable d)	{ 		if(c==exit) { destroyApp(true); notifyDestroyed(); 		} else {	 		new Thread(new Runnable() { 				public void run() { 				try { HttpConnectionconn = null; 					String url = "http://www.burrp.com/robots.txt"; InputStream is = null; 					try {
Continued conn = (HttpConnection)Connector.open(url); conn.setRequestMethod(HttpConnection.GET); conn.setRequestProperty("User-Agent","Profile/MIDP-2.1 Confirguration/CLDC-1.1"); intrespCode = conn.getResponseCode(); 	if (respCode == conn.HTTP_OK)  { StringBuffersb = new StringBuffer(); 		is = conn.openDataInputStream(); intchr; 		while ((chr = is.read()) != -1) sb.append((char) chr);						form.append("Here is the records from www.burrp.com: " + sb.toString()); 	} else { System.out.println("Error in opening HTTP Connection. Error#" + respCode); 	} 	} 	catch(Exception e) { System.out.println(e); 	}
Continued 	finally { 	try { 		if(is!= null) is.close(); 		if(conn != null) conn.close(); 	} 	catch(Exception e) { System.out.println(e); 	} 	} 	} 	catch(Exception e) {	 System.out.println(e); 	} 	} 	} 	).start(); } }
Thank You

Contenu connexe

Tendances

Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
leminhvuong
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
Anton Arhipov
 
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Ryosuke Uchitate
 

Tendances (20)

Java practical
Java practicalJava practical
Java practical
 
Modern Java Development
Modern Java DevelopmentModern Java Development
Modern Java Development
 
Backendless apps
Backendless appsBackendless apps
Backendless apps
 
Android - Saving data
Android - Saving dataAndroid - Saving data
Android - Saving data
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Simple API for XML
Simple API for XMLSimple API for XML
Simple API for XML
 
Graphical User Components Part 1
Graphical User Components Part 1Graphical User Components Part 1
Graphical User Components Part 1
 
Introdução à programação orientada para aspectos
Introdução à programação orientada para aspectosIntrodução à programação orientada para aspectos
Introdução à programação orientada para aspectos
 
Java swing
Java swingJava swing
Java swing
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Code generation for alternative languages
Code generation for alternative languagesCode generation for alternative languages
Code generation for alternative languages
 
Java swing
Java swingJava swing
Java swing
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
 
JDI 2.0. Not only UI testing
JDI 2.0. Not only UI testingJDI 2.0. Not only UI testing
JDI 2.0. Not only UI testing
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
 
Oracle 10g
Oracle 10gOracle 10g
Oracle 10g
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
 
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!Amazon Cognito使って認証したい?それならSpring Security使いましょう!
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
 
04b swing tutorial
04b swing tutorial04b swing tutorial
04b swing tutorial
 
Swing
SwingSwing
Swing
 

Similaire à J2ME Lwuit, Storage & Connections (Ft Prasanjit Dey)

Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
Jussi Pohjolainen
 
Top 3 SWT Exceptions
Top 3 SWT ExceptionsTop 3 SWT Exceptions
Top 3 SWT Exceptions
Lakshmi Priya
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorial
sumitjoshi01
 

Similaire à J2ME Lwuit, Storage & Connections (Ft Prasanjit Dey) (20)

Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
 
Introduction To Google Android (Ft Rohan Bomle)
Introduction To Google Android (Ft Rohan Bomle)Introduction To Google Android (Ft Rohan Bomle)
Introduction To Google Android (Ft Rohan Bomle)
 
F# And Silverlight
F# And SilverlightF# And Silverlight
F# And Silverlight
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 
Google Web Toolkit
Google Web ToolkitGoogle Web Toolkit
Google Web Toolkit
 
Guide to Generate Extent Report in Kotlin
Guide to Generate Extent Report in KotlinGuide to Generate Extent Report in Kotlin
Guide to Generate Extent Report in Kotlin
 
Android best practices
Android best practicesAndroid best practices
Android best practices
 
Dojo and Adobe AIR
Dojo and Adobe AIRDojo and Adobe AIR
Dojo and Adobe AIR
 
Struts 2 + Spring
Struts 2 + SpringStruts 2 + Spring
Struts 2 + Spring
 
Top 3 SWT Exceptions
Top 3 SWT ExceptionsTop 3 SWT Exceptions
Top 3 SWT Exceptions
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorial
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
 
What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.
 
How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
 
Open Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 TutorialOpen Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 Tutorial
 
package org dev
package org devpackage org dev
package org dev
 
Package org dev
Package org devPackage org dev
Package org dev
 
YUI 3
YUI 3YUI 3
YUI 3
 

Dernier

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 

J2ME Lwuit, Storage & Connections (Ft Prasanjit Dey)

  • 1. JAVA ME LWUIT, STORAGE & CONNECTIONS Fafadia-Tech PrasanjitDey prasanjit@fafadia-tech.com
  • 2. LWUIT The Lightweight User Interface Toolkit (LWUIT) is a versatile and compact API for creating attractive application user interfaces for mobile devices LWUIT provides sophisticated Swing-like capabilities without the tremendous power and complexity of Swing LWUIT offers a basic set of components, flexible layouts, style and theming, animated screen transitions, and a simple and useful event-handling mechanism LWUIT is developed by Sun Microsystems and is inspired by Swing
  • 3. Key Features Layouts Manager Pluggable Look and Feel & Themes Fonts Touch Screen Animations & Transitions 3D and SVG Graphics Integration Tools Bi-directional text support
  • 4. Simple Lwuit Program import javax.microedition.midlet.*; import com.sun.lwuit.*; // imports LWUIT import com.sun.lwuit.layouts.BorderLayout; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; import java.io.IOException; public class Hello extends MIDlet implements ActionListener { public void startApp() { Display.init(this); // initializes the display try { // using a theme here Resources r = Resources.open("/res/ThemeJava1.res"); UIManager.getInstance().setThemeProps(r.getTheme("ThemeJava1")); } catch (IOExceptionioe) { // Do something here. }
  • 5. Continued Form f = new Form("Hello, LWUIT!"); f.addComponent("Center",new Label("Prasanjit's j2me apps ")); f.show(); // display the form Command exitCommand = new Command("Exit"); f.addCommand(exitCommand); f.setCommandListener(this); } public void pauseApp() { } public void destroyApp(boolean unconditional) {} public void actionPerformed(ActionEventae) { notifyDestroyed(); } }
  • 6. Output Download the LWUIT zip from http://sun.java.com/javame/technology/lwuit and add it to your project /resources directory, also add all your themes, images and other resources in a folder, zip it and add to your project/resources directory.
  • 7. Simple Layout Example import javax.microedition.midlet.*; import com.sun.lwuit.layouts.BoxLayout; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; import com.sun.lwuit.events.ActionListener; import com.sun.lwuit.events.ActionEvent; import java.io.IOException; public class Layouts extends MIDlet implements ActionListener { Form form; Command exit; public void startApp() { Display.init(this); try { Resources r = Resources.open("/res1/ThemeJava2.res"); UIManager.getInstance().setThemeProps(r.getTheme("ThemeJava2")); } catch(IOExceptionioe) { System.out.println(ioe); } form = new Form("Layouts ");
  • 8. Continued // adding 5 buttons on the form along with a command Container buttonBar = new Container(new BoxLayout(BoxLayout.X_AXIS)); buttonBar.addComponent(new Button("Add")); buttonBar.addComponent(new Button("Remove")); buttonBar.addComponent(new Button("Edit")); buttonBar.addComponent(new Button("Send")); buttonBar.addComponent(new Button("Exit")); exit = new Command("Exit"); form.addComponent(buttonBar); // buttonBar is a container for other buttons form.addCommand(exit); form.setCommandListener(this); form.show(); public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void actionPerformed(ActionEventae) { notifyDestroyed(); } }
  • 9. Simple Event Handling import javax.microedition.midlet.*; import java.io.IOException; import com.sun.lwuit.*; import com.sun.lwuit.events.*; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; public class EventHandling extends MIDlet implements ActionListener { Form form; int c = 1000; Label l1; Button b1,b2; public void startApp() { Display.init(this); try { Resources r = Resources.open("/res1/ThemeJava2.res"); UIManager.getInstance().setThemeProps(r.getTheme("ThemeJava2")); } catch(IOExceptionioe) { System.out.println(ioe); }
  • 10. Continued Form = new Form("Event handling"); l1 = new Label(" "); b1 = new Button("Change label to add "); form.addComponent(b1); form.addComponent(l1); form.show(); b1.addActionListener(this); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void actionPerformed(ActionEventae) { c++; l1.setText("" +c); form.layoutContainer(); } }
  • 11. Storage Record Management System or rms is used to provide the storage capabilities in Java ME It stores records in binary format inside the Record Stores Data is not lost even if the device is switched off rms provides various methods for storing and retrieving records: openRecordStore() closeRecordStore() deleteRecordStore() getRecord() enumerateRecords()
  • 12. rms Example import java.io.*; import javax.microedition.midlet.*; import javax.microedition.rms.*; // imports all rms resources public class rmsDemo extends MIDlet { intsize_available, id; RecordStorers; public rmsDemo() { openStore(); // open recordstore addRecord(); // adds record into recordstore getRecord(); // gets all rcords closeStore(); // closes the recordstore } public void startApp() {} public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void openStore() { try { rs=RecordStore.openRecordStore("names",true); size_available=rs.getSizeAvailable(); System.out.println("Start"); System.out.println("Available size is " +size_available); }
  • 13. Continued catch(Exception e) { System.out.println(e); } } public void closeStore() { try { rs.closeRecordStore(); } catch(Exception e) { System.out.println(e); } } public void addRecord() { try { String record="java ME persistent storage"; byte data[]=record.getBytes(); // converts into bytes of data id=rs.addRecord(data,0,data.length); } catch(Exception e) { System.out.println(e); } }
  • 14. Continued public void getRecord() { try { byte getData[]= rs.getRecord(id); // gets the record System.out.print("Records in byte format: "); for(int j=0;j<getData.length;j++) { System.out.print(getData[j]); System.out.print(" "); } System.out.println(); System.out.print("Records in string format: "); for(inti=0;i<getData.length;i++) { System.out.print(new String(new byte[]{getData[i]})); } System.out.println(); System.out.println("Done with it"); } catch(Exception e) { System.out.println(e); } } }
  • 15. Connections All the important classes and methods for connecting the to the wireless network are included in the javax.microedition.io.* package The connections types are provided by the InputStream and the OutputStream interfaces These interfaces adds the ability to input and output data over the network There are three important level of connections available Socket Datagram HTTP connection
  • 16. Http Example In this program, we create a form and add a command to it. On clicking the command, the data from the url is displayed on the device. Here is the actual thread to display the data on the device. public void commandAction(Command c, Displayable d) { if(c==exit) { destroyApp(true); notifyDestroyed(); } else { new Thread(new Runnable() { public void run() { try { HttpConnectionconn = null; String url = "http://www.burrp.com/robots.txt"; InputStream is = null; try {
  • 17. Continued conn = (HttpConnection)Connector.open(url); conn.setRequestMethod(HttpConnection.GET); conn.setRequestProperty("User-Agent","Profile/MIDP-2.1 Confirguration/CLDC-1.1"); intrespCode = conn.getResponseCode(); if (respCode == conn.HTTP_OK) { StringBuffersb = new StringBuffer(); is = conn.openDataInputStream(); intchr; while ((chr = is.read()) != -1) sb.append((char) chr); form.append("Here is the records from www.burrp.com: " + sb.toString()); } else { System.out.println("Error in opening HTTP Connection. Error#" + respCode); } } catch(Exception e) { System.out.println(e); }
  • 18. Continued finally { try { if(is!= null) is.close(); if(conn != null) conn.close(); } catch(Exception e) { System.out.println(e); } } } catch(Exception e) { System.out.println(e); } } } ).start(); } }