Log Manager History Chatting and File Transfer using J2ME


When you want to record some events that have been occured like Chatting or Downloading/Uploading a file in your mobile phone, you must a storage to save the history. You can use a Record Store to save it.

On this post I will give a solution.

/*
* Nama File : LogManager.java
* Deskripsi : Untuk menyimpan dan menampilkan semua sejarah percakapan dan transfer file
* Penulis : Kelompok SDPM – 2008 Modul Log Manager
*/

package org.pidel.LogManager;
import javax.microedition.rms.*;

public class LogManager
{
private RecordStore rs;
private RecordEnumeration re = null;
private String rsName;

public LogManager() {}

public void setRecordStoreName(String _rsName)
{
rsName = _rsName;
}

public String getRecordStoreName()
{
return rsName;
}

public boolean openConnection()
{
try
{
rs = RecordStore.openRecordStore(rsName, true);
return true;
}
catch(Exception e)
{
return false;
}
}

public boolean closeConnection()
{
try
{
rs.closeRecordStore();
return true;
}
catch (Exception e)
{
return false;
}
}

public boolean clearHistory()
{
try
{
rs.deleteRecordStore(rsName);
return true;
}
catch(Exception e)
{
return false;
}
}

public boolean saveHistory(String strInput)
{
byte [] record = strInput.getBytes();

try
{
int recID = rs.addRecord(record, 0, record.length);
return true;
}
catch (Exception e)
{
return false;
}
}

public String loadHistory()
{
StringBuffer buffer = new StringBuffer();
String recordFill = “No history found until now …”;

try
{
Comparator comparator = new Comparator();
re = rs.enumerateRecords(null, comparator, false);

while(re.hasNextElement())
{
buffer.append(new String(re.nextRecord()));
buffer.append(”#”);
}
re.destroy();
}
catch (Exception e)
{
recordFill = null;
}

return recordFill = buffer.toString();
}
}

But the above code is a component so when you want to use it you must import the package.

You can explore this example Demo:

/*
* Nama File : LogManagerDemo.java
* Deskripsi : Demo dari penggunaan LogManager
* Penulis : Kelompok SDPM – 2008 Modul Log Manager
*/

import javax.microedition.midlet.MIDlet;
import org.pidel.LogManager.*;

public class LogManagerDemo extends MIDlet
{
public LogManagerDemo() {}

public void startApp()
{
LogManager LM = new LogManager();
LM.setRecordStoreName(”RS_FILE”);
LM.openConnection();
LM.saveHistory(”Session Logged”);
System.out.println(LM.loadHistory());
LM.closeConnection();
LM.clearHistory()
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}
}

Iklan

2 comments

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s