Simple Example

In the most stripped-down form, all that you need to display an embedded browser field is to create the BrowserContent, hand it a connection to your web site, and minimally implement the RenderingApplication interface. Listing 7-2 shows a basic class that does just this by displaying the Google home page in the middle of your own screen. By default, the browser Field will fill the entire screen; since this is not what we want, we create a custom Manager that forces the browser to be only half as tall as the screen and slightly narrower. The StatusUpdater class is borrowed from the MediaGrabber app examples, and allows us to watch what is happening behind the scenes as the browser loads.

Listing 7-2. Embedding a Browser in a MainScreen import java.io.IOException; import java.util.*;

import javax.microedition.io.*;

import net.rim.device.api.browser.field.*; import net.rim.device.api.io.http.HttpHeaders; import net.rim.device.api.system.Application; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.LabelField; import net.rim.device.api.ui.container.*;

public class BrowserScreen extends MainScreen implements Runnable, RenderingApplication

private RenderingSession renderSession; private LabelField status; private StatusUpdater updater; private String url;

public BrowserScreen() {

renderSession = RenderingSession.getNewInstance(); status = new LabelField("Loading..."); add(status);

updater = new StatusUpdater(status); url = "http://www.google.com"; (new Thread(this)).start();

private class BrowserFieldContainer extends VerticalFieldManager {

public BrowserFieldContainer() {

super(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Manager.FIELD_HCENTER);

public void sublayout(int maxWidth, int maxHeight) {

int width = BrowserScreen.this.getWidth(); int height = BrowserScreen.this.getHeight(); super.sublayout((int) (width * .9), height / 2);

HttpConnection conn = null;

conn = (HttpConnection) Connector.open(url); updater.sendDelayedMessage("Connection opened");

BrowserContent browserContent = renderSession.getBrowserContent( conn, this, null);

Field field = browserContent.getDisplayableContent();

synchronized (Application.getEventLock()) {

deleteAll(); add(status);

add(new LabelField("Your search starts here.")); BrowserFieldContainer container =

new BrowserFieldContainer(); container.add(field); add(container);

add(new LabelField("Don't forget to tip the service!"));

browserContent.finishLoading();

catch (Exception e) {

updater.sendDelayedMessage(e.getMessage());

finally {

catch (Exception e) {

public Object eventOccurred(Event event) {

updater.sendDelayedMessage("Handle event " + event.getUID() + " for "

+ event.getSourceURL()); return null;

public int getAvailableHeight(BrowserContent browserContent) {

public int getAvailableWidth(BrowserContent browserContent) {

public String getHTTPCookie(String url) return null;

public int getHistoryPosition(BrowserContent browserContent) return 0;

public HttpConnection getResource(RequestedResource resource, BrowserContent referrer)

return null;

public void invokeRunnable(Runnable runnable) (new Thread(runnable)).start();

Tip: Application.getEventLock() is a static method useful in situations when you are working in a non-UI thread and want to update some UI elements before continuing to process in your separate thread. It is more compact and efficient than using Application.invokeLater() followed by creating and starting a new Thread.

If you create a simple app that pushes this screen and run the example, you will notice that the Google page partially loads, but, as Figure 7-5 shows, no images are displayed. Furthermore, if you try to click any links or type in a search, nothing will happen. Clearly, there is more to be done. On the positive side, everything just works from a UI perspective. If you are using a trackball-based device, you'll see that links automatically take focus as you scroll across them, and you can type in the embedded search field by highlighting it. On a touch-based device, links highlight as you touch them, and you can scroll the field by dragging your finger around.

Net Rim Device Api Component
Figure 7-5. A very simple browser field

Was this article helpful?

0 0

Post a comment