Over a million developers have joined DZone.

Auto-update of Midlets

DZone's Guide to

Auto-update of Midlets

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Any professional application should be capable of updating itself over the internet. Even Midlets! The idea is fairly easy in that the Midlet needs to make a call to the server to check what version the latest software has, and compare that to the installed version. If there is a newer version available, then it needs to start the (mobile) device's browser and point it at the URL of the new JAD file. The device will take care of the download and installation after that. So, the following article shows you exactly how this can be implemented.

First off, refer to the previous article, which described a framework for making server calls.

The sequence diagram looks like this:

Based on the framework, the following JSP can be installed serverside, which reads the version number out of the JAD file:

<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.RandomAccessFile"%>
<%@page import="java.io.File"%>
<%@page import="java.util.List"%>
RandomAccessFile raf = null;
	String version = null;
	String path = request.getSession().getServletContext().getRealPath("index.jsp");
	File f = new File(path);
	f = f.getParentFile();
	f = new File(f, "jads/nameOfJad.jad");
	raf = new RandomAccessFile(f, "r");
	String curr = null;
	while((curr = raf.readLine()) != null){
			version = curr.substring(16);

	add other master data like the users details, their roles, etc. here...

}catch(Exception e){
	log.warn("failed to read master data", e);
	<%=e.getMessage() %>
	if(raf != null) raf.close();


This JSP is called when the Midlet starts, and effectively delivers "master data" to the device, such as the users roles, account details and importantly, the version number of the latest available JAD file on the server. Based on this version number, which is read as the MIDlet-Version property out of the JAD on the server, the client can decide if the currently installed version is up to date. The Midlet does this by calling the getAppProperty(String) method, passing it the String "MIDlet-Version", which reads the version of the local JAD, that was used to install the current version.

Once the client has decided that there is a newer version available, it can ask the user if they wish to update by making an Alert the current screen. If the user chooses to install the update now, then all that is left is to open the devices browser to point at the new JAD which is online, and then close the application. Putting it together in a convenient method gives the following:

private void checkForUpdate(){
final String currentVersion = getAppProperty("MIDlet-Version"); //eg 1.0.62
new MasterDataGetter(currentVersion, model, this) {
public void onSuccess(Object md) {
MasterData masterData = (MasterData) md;

if(currentVersion != null && masterData.getLastestVersion() != null){
if(isUpdateAvailable(masterData.getLastestVersion(), currentVersion)){
//does the user want to?

Alert a = new Alert(
"An update is available (version " + masterData.getLastestVersion() + ".\r\n" +
"Would you like to download and install it now?");
final Command no = new Command("No", Command.ITEM, 1);
final Command yes = new Command("Yes", Command.ITEM, 2);
a.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if(c == yes){
try {
platformRequest(Constants.getBaseUrl() + "jads/nameOfJad.jad");
destroyApp(true); //coz we are performing an update!
} catch (ConnectionNotFoundException e) {
//tough titties :-( ie platformRequest couldnt be called
handleException("Failed to open browser to get installation files.", e, AlertType.ERROR);
} catch (MIDletStateChangeException e) {
handleException("Failed to destroy midlet", e, AlertType.ERROR);
} finally {
}else if(c == no){
Display.getDisplay(Main.this).setCurrent(a, currentView);

} //endif is update available?
} //endif neither version is null

//now we have already called maxant, and the user has confirmed that they are
//happy to go online (eg BlackBerry shows them the URL of the first connection
//attempt), we can continue to GA. we are trying to hide this fact from the user
//as it may put of some users.

}//end #onSuccess

public void onError(int code, String result) {
if(model.isShowDebugInfo()) sendDebugLog.callServer("error with update check " + code + ": " + result);
//tough titties, dont offer to upgrade


From http://blog.maxant.co.uk

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}