Java- I/O, SMS etc N Amanquah.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Advertisements

Java ME persistence made easy! by Thiago Rossato Thiago Moreira.
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Cosc 4730 Blackberry: Record Store & SQLite. Introduction RecordStore – Comes from JavaME, MIDP 1.0 On some phone who where you may not have a filesystem.
Files and Streams CS 21a Chapter 11 of Horstmann.
Developing Software for Wireless Devices
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Persistent Storage  Record Stores  mini databases – represent data as byte records  Record Management System (RMS) API  Files and Directories  FileConnection.
Detecting Changes  ItemStateListener interface – detect changes in internal state of an Item  new selection made in a ChoiceGroup  adjusted value of.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Io package as Java’s basic I/O system continue’d.
J2ME Messaging Khanh Le. Objective  The objective of wireless messaging is to extend the networking and I/O capabilities of J2ME applications to send.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Java Mobile Application sms,sim,mms and barcode application Presented by Ayedh(SIM and MMS) Asad(SMS and Barcode Application)
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
File I/O 11_file_processing.ppt
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
BlackBerry Persistent Storage Models Persistent Storage APIs and Record Management System.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 8 CDC andJ2ME Rob Pooley
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
PROG Mobile Java Application Development PROG Mobile Java Application Development Data Storage, Continued.
MIDP Programming Networking. Chapter Objectives The CLDC Streams Model Generic Connection Framework (GCF) Supported Protocols Creating a Connection Review.
Chapter - 11 Introduction to File and Streams This chapter includes -  Defining a File  Testing and Checking File Objects  Accessing File Objects.
MIDP Database Programming Using RMS: Jingwu He Csc 3360.
Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
CS1020 Data Structures and Algorithms I Lecture Note #16 File Processing.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
The Record Store ( ) Frank Ducrest. The Record Store 2 The Record Store provides persistence of data between MIDlet runs not quite a DBMS API.
Chapter 10: I/O Streams Input Streams Output Streams
Sequential files creation & writing
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Object-Orientated Analysis, Design and Programming
Running a Forms Developer Application
Threads in Java Two ways to start a thread
Android Application Data Storage 1.
3 Introduction to Classes and Objects.
Program Input/Output (I/O)
Java Review Rem Collier.
12: The Java I/O System stream model.
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
Pre-assessment Questions
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
I/O Basics.
Chapter 13: File Input and Output
Unit 6 Working with files. Unit 6 Working with files.
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Sadalage & Fowler (Amazon)
Web Design & Development Lecture 8
LCC 6310 Computation as an Expressive Medium
David Davenport Spring 2005
Presentation transcript:

Java- I/O, SMS etc N Amanquah

File I/O RecordStore SMS Assorted

FileI/O- output –chap 15 FileConnection API of the GCF provides the streams for I/O String url=“file:///root1/photos/myfile” FileConnection fc = (FileConnection)Connector.open(url); try { if (!fc.exists()) fc.create(); OutputStream rawOut = fc.openOutputStream(); PrintStream out = new PrintStream(rawOut); try { out.println(“Hello into a file"); } finally { out.close(); } } finally { fc.close(); }

FileI/O-input String url=“file:///root1/photos/myfile” FileConnection fc = (FileConnection)Connector.open(url); try { Inputstream in =fc.openInputStream(); //in.read() //read from file } finally { in.close(); } finally { fc.close(); }

File I/O See API for other interesting functions Install app by OTA to have private directory for midlet created by emulator. Access to system folders: String system.getProperty(“fileconn.dir.photos”) If returns a directory URL or NULL fileconn.dir.videos fileconn.dir.graphics fileconn.dir.tones fileconn.dir.music fileconn.dir.recordings

File I/O Example –kb 15 illustrates creating a directory, file, write & read, remove file & dir. Application must be signed to suppress all the notifications Fileconnection is cleaner, consistent on most devices Can access images, sounds, other files App must be signed.(permissions)

RecordStore –chap 14 Recordstore data is for midlet suite. (by default) RecordStore rs=RecordStore.openRecordStore(“name”, true) //true= create if one does not exist Write: Byte[] data… Int id=rs.addRecord(data, 0,data.length) Read: Byte[] rec=rs.getRecord(id) //read a particular record

RecordStore while (re.hasNextElement()) { To obtain id to use: RecordEnumeration re=rs.enumerateRecords(null,null,false) 3 Params: recordFilter, recordComparator, keepCurrentIfChanged? To fetch all records, use: while (re.hasNextElement()) { byte[] record = re.nextRecord(); ….//process the records See kb 14: in following code, store and read back key-value pairs

RecordStore-write RecordStore rs = RecordStore.openRecordStore(mName, true); try { Enumeration keys = mMap.keys(); while (keys.hasMoreElements()) { String key = (String)keys.nextElement(); String value = (String)mMap.get(key); byte[] record = packRecord(key, value); rs.addRecord(record, 0, record.length); } catch (IOException ioe) { System.out.println("Error packing in " + mName + " : " + key + " -> " + value); finally { rs.closeRecordStore(); } private byte[] packRecord(String key, String value) throws IOException { ByteArrayOutputStream raw = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(raw); out.writeUTF(key); out.writeUTF(value); return raw.toByteArray();

RecordStore-read private void read() throws RecordStoreException { RecordStore rs = RecordStore.openRecordStore(mName, true); mMap = new Hashtable(); try { RecordEnumeration re = rs.enumerateRecords(null, null, false); while (re.hasNextElement()) { byte[] record = re.nextRecord(); parseRecord(record); } finally { rs.closeRecordStore(); } private void parseRecord(byte[] record) { ByteArrayInputStream raw = new ByteArrayInputStream(record); DataInputStream in = new DataInputStream(raw); String key = in.readUTF(); String value = in.readUTF(); mMap.put(key, value); catch (IOException ioe) { System.out.println("Bad record in " + mName + ".");

Messaging –chap 19 WMA =wireless messaging API, both sms & mms Based on the GCF, use the Connetor to get MessageConnection Import javax.wireless.messaging 4 lines of code to send sms obtain a connector Get a message with newMessage() Fill the message ie setPayLoad() Send()

Sending SMS import javax.wireless.messaging Public void sendText(String addr, String text) throws IOException, InterruptedException{ String cs=“sms://”+addr + port; MessageConnection mc=(MessageConnection)Connector.open(cs); TextMessage tm=(TextMessage)mc.newMessage(MessageConnection. TEXT_MESSSAGE); tm.setPayloadText(text); mc.send(tm) } //NB: port number is optional, if omitted= normal text msg

Receiving SMS Create a MessageConnection without a destination string Put application in push registry, (with a static registration) Two options: Loop on MessageConnection ‘s receive() method (thread) (it blocks) Use a thread to listen for incoming messages. Register a listener. Its notifyIncomingMessage() will be called See kb-19

Push registry Allows incoming network connection to launch up application To register, provide Connection string eg sms://:50000 A class name that describes the midlet to be launched. String representing allowed senders (use * for all connections) Dynamic registration – see chapter 6 To register: Make the ff calls from within a thread: PushRegistry.registerConnection(connStr, midletName, “*”) To unregister: PushRegistry.unregisterConnection(connStr) //only one param

Push registry- static registration Go to project properties Application Descriptor push registry add. To find incomming connections, call PushRegistry’s listConnections(), pass false to get every input Returns array of connections. Run kb-06 OTA whether static or dyanamic registration is used. Use the WMA utility console to send sms to the emulator

Push registry- timed execution Dynamic ! Call PushRegistry.registerAlarm(“midletname”, long_time) Do this in a thread, ie the registration. Run kb-06 OTA

Other topics Cool methods: boolean Display. flashBacklight(int duration) boolean Display. vibrate(int duration) (both return false if feature is not supported, duration in ms) Contacts & Calendar: chapter 16 of kickbutt book Parsing XML: chapter 21 Parsing RSS: chapter 21

Use of Netbeans Drag and Drop approach to app development

Lab work Build an application that does the following: Make use of netbeans: Fetch data from the user (the user’s preferences) Send it by sms to another number. Store the data you have collected in a record store so that it can be retrieved the next time the user runs the program. One menu item should allow the user to enter the values afresh, or to fetch the saved values.