Sunday, July 20, 2008

Run mobile browser or any other application or services from Midlet

What I wanted to do... I tried to communicate between mobile browser and an mobile application. Then I got the method "javax.microedition.midlet.MIDlet.platformRequest()".

The platformRequest() method takes one input argument, a String URL that it passes to the platform's application management software (AMS).The AMS examines the scheme of the URL and decide which application to be invoked. This method returns TRUE if the current application must exit to run the new application or service otherwise returns FALSE.And it throws a ConnectionNotFoundException if the platform cannot handle the URL argument.

J2ME Wireless Toolkit Support:
What you have to do is just add the following line in the system.config file of toolkit's lib directory.
com.sun.midp.midlet.platformRequestCommand:"C:\Program Files\Mozilla Firefox\firefox.exe"


Now its ready to test that method. Souce Code:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class platformRequestExample extends MIDlet implements CommandListener {

private TextBox mTextBox;
private Command URLCommand;
private Command ExitCommand;
private Command OKCommand;

public platformRequestExample() {
mTextBox = new TextBox("URL:", "", 100, TextField.URL);

URLCommand = new Command("URL", Command.SCREEN, 1);
ExitCommand = new Command("Exit", Command.EXIT, 0);
OKCommand = new Command("Submit", Command.OK, 0);

mTextBox.addCommand(ExitCommand);
mTextBox.addCommand(OKCommand);
mTextBox.addCommand(URLCommand);
mTextBox.setCommandListener(this);
}

public void startApp() {
Display.getDisplay(this).setCurrent(mTextBox);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
boolean flag;
if (c.getCommandType() == Command.EXIT) {
notifyDestroyed();
} else if (c == OKCommand) {
try {
flag=platformRequest(mTextBox.getString()); // open the wap browser.
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else if (c == URLCommand) {
mTextBox.setConstraints(TextField.URL);
mTextBox.setTitle("Enter a URL:");
mTextBox.setString("http://www.google.com.bd/search?hl=en&q=TEST");
}
}

}

No comments:

search engine

Custom Search