J2ME Record Management Storage

xiaoxiao2021-03-06  105

Mobile Information Device Profile - Mobile Java Application Platform - Provides a mechanism for MIDP applications that span multiple calls lasting storage data. This persistent storage mechanism can be seen as a simple recorded database model called record management system (RMS). Here, Soma Ghosh describes how your J2ME application can use RMS to manage and interpret data. With a sample telegram database, you will also learn about this concept. The J2ME Record Management System J2ME Recording Management System (RMS) provides a mechanism, with this mechanism, the MIDlet can last for data and retrieve data later. In a recorded method, J2ME RMS is composed of a plurality of records. You can imagine each recording store into a record set, which will take a long time across multiple MIDlet. The equipment platform is responsible for the entire process of the platform (including restart, changing battery, etc.), and performs the integrity of the MIDlet record storage. Record is created stored in a platform-related location (such as a non-volatile device memory), which is not directly public to the MIDlet. The RMS class calls the platform-specific local code, which uses the standard OS data manager function to perform the actual database operation. Recording storage implementation ensures that all individual record storage operations are atomic, synchronized, and serialized, so multiple access will not occur. Record the storage of the lid to indicate that it is last modified last time. Record the storage also maintains the version (Version), it is an integer that modifies the operation of the record storage content, this number plus one. Version and timestamp are useful for synchronization purposes. When the MIDlet uses multiple threads to access a record storage, coordinate the access is the responsibility of the MIDlet; if it can't do this, it may have an unexpected result. Similarly, if a platform uses a plurality of threads that try to simultaneously access the record store, the record storage is performed on the record storage of the MIDLET and its synchronization engine. Each record in the record store is an array of bytes and has a unique integer identifier. The management device database javax.microedition.rms.RecordStore class represents the RMS record storage. It provides several ways to manage and insert, update, and delete records in recording stores. Manage Record Storage To open a record storage, call the openRecordStore () method for javax.microedition.rms.RecordStore. Public Static RecordStore OpenRecordStore (String RecordStoreName, Boolean CreateifNessary) Opens a record storage that specifies the name RECORDSTORENAME. If there is no record storage with this name, then this method is called to create one. If the recording store has been opened, this method will return a reference to the same record storage object. Listing 1. Open a RecordStore RecordStore RS = RecordStore.OpenRecordStore ("MyAppointments", true); once all the operation is complete, the call to the closerecordStore () will close the record storage of the specified name. Further operations cannot be performed when a recording store is turned off.

Listing 2. Close a RecordStore Rs.closecordStore (); you can delete the record storage of the specified name by calling the deleteRecordStore () method. Listing 3. Delete a RecordStore RecordStore.deleteRecordStore ("MyAppointments"); insert the record MIDlet call the Javax.microedition.rms.RecordStore class's addRecord () method to insert a new record into the recording store. This is a blocking atomic operation and returns a new recorded RecordID. The record is written to a persistent storage before this method returns. Public int addrecord (byte [] data, int offset, int number is inserted into a record from the byte array DATA, which is based on its starting index, Numbytes as its length. Listing 4. Insert a record string app = "new record"; BYTE BYTES [] = appt.getBytes (); rs.addrecord (bytes, 0, bytes.length); Update Record Update A special record includes getting this recorded handle And set new information. Public int getRecord (int RecordID, Byte [] buffer, int offset returns data stored in the byte array represented by Buffer. Public Byte [] getRecord (int Recorded) Returns a copy of the data represented by RecordID. Public void setRecord (int Recordid, Byte [] newData, int offset, int numbertes) Set new information in the location represented by RecordID, the new information is based on its starting index as its starting index, and the word Numbytes as its length Newdata. Listing 5. Update a record string newAppt = "update record"; byte data = newappt.getbytes (); rs.seTRecord (1, data, 0, data.length ()); Delete Record MIDlet calls the deleteRecord () method to Remove the record in the store. Public void deleteRecord (int Recordid) Removes records represented by RecordID. This recorded RecordID will not be reused. Listing 6. Delete a record RS.DeleteRecord (1); Data Interpretation J2ME API provides an interface to interpret data stored in the record store. This process includes comparison records to determine their relative sorting. It also includes filtration based on the content of the given condition. The comparison record MIDlet implementation RECORDComparator interface, and defines Compare (Byte [] REC1, BYTE [] REC2) method to compare two candidate records. The return value of this method must indicate the order of these two records.

Listing 7. Compare records and determine the relative sorting int compare (byte [] b1, byte [] b2) {string s1 = new string (b1); string s2 = new string (b2); if (S1.Compareto (S2)> 0) RETURN Recordcomparator.Follows; Else IF (S1.Compareto (S2) == 0) Return RecordComparator.Equivalent; Else Return RecordComparator.precedes;} Enumeration record RecordenumeRATION interface is responsible for enumerating records in the record. It is logically maintained a recorded recorded recordID in the record store. The enumerator will iterate all records (or if an optional recording filter is provided) is provided. If there is neither a specified filter does not specify a comparator, enumeration will traverse all records in the record store in undefined order. Listing 8. Enumeration Recording RecordenuMeration Re = Rs.EnumerateRecords (NULL, NULL, FALSE); if (Re.hasNexTelement ()) Byte Next System [] = Re.nexTrecord (); Filter Record MIDLET Implementing the Recordfilter interface, defining check records Meet the standard filter defined by the application. This application implements the recordfilter's Match () method to select RecordenumeRATION. Listing 9. Filter recording public boolean matches (Byte [] Candidate) {string s1 = new string (Candidate); if (S1.Equals ("xx")) Returns true; Else Returns false;} Development Telephone Dating Book We will explain the functionality of J2ME RMS by building a calling book. This app will allow users to set a date and time date, cancel date or view the list of dating already set. The full list of various screens that make up this application and the user interface element of the screen element can be obtained in the MID framework API documentation provided with J2ME Wireless Toolkit; for more details on these elements, please check my earlier A article written to DeveloperWorks (see the reference portion below for these two links). Recording storage can be stored in byte streaming. In our application, the date and time of the user entered into a string, converted into bytes, and then stored. Listing 10. Add a new date to the database public boolean matches (Byte [] Candidate) string appt = apptname " appttime; byte bytes [] = appt.getbytes (); rs.addrecord (bytes, 0, BYTES.LENGTH); Similarly, this application retrieves records in byte stream, and then converts it into a string. This string is formatted in #### aaaa, where # represents the number representing time information, and AAAA represents the characters that represent the appointment. This application resolves this string to get date and time information, display them in the format desired, such as Description - MM / DD / YYYY HH: MM AM_PM.

Listing 11. Retrieve a record from record storage BYTE B [] = r.getRecord (j); string str = new string (b, 0, b.length); Listing 12. Listing 12. Resolving data obtained from log storage, Then display IF in the format desired by the user (CHARACTER.ISDIGIT)) AT = Str.Charat (I); Else Name = Str.Charat (i); time = long.parselong (AT ); java.util.date date = new java.util.date (time); java.util.calendar rightnow = java.util.calendar.GetInstance (); RightNow.SetTime (date); string year = string.valueof Rightnow.get (java.util.calendar.year); string month = String.Valueof (rightnow.util.calendar.month) 1); string day = string.valueof (RightNow.get (Java) Util.calendar.date)); string displayName = name "-" " " " DAY; user is allowed to select some date from the recording store and delete them from the recording store. Because the RecordID deleted in order to maintain the original order in the record cannot be reused, this record is marked by the unique string mode as invalid. Listing 13. Mark a record as deleted string deactive = "@"; byte b [] = deactive.getbytes (); rs.seTRecord (M 1, B, 0, B.Length); When this application displays When an appointment list, it detects those string modes that cannot be recorded and then skip them. Listing 14. Skip invalid record if (! ("))) {// record is valid} else {// record is invalid.} An important aspect of this application is the user interface. Various screens have the following: Welcome Form: Welcome form shows a list of dating already set, if there is no setting, notify the user. It provides various options to continue or exit this application. Menu Form: The menu form provides the user with the viewing appointment, setting an option or cancellation of the appointment. Display Form: The display form displays a list of dating already set. Settings a form: Setup form provides a date selection domain and an input field to provide details of the new appointment. This information is stored in the database when the user selects saved. Delete a form: Delete a form lists a set of dates and provides options to select one or more. If the user selects the deletion, the selected group is marked as invalid in the recording store. Application implementation enables yourself to respond to CommandListener and ItemStateListener interfaces of various events.

转载请注明原文地址:https://www.9cbs.com/read-100572.html

New Post(0)