views:

676

answers:

2

Hello,

I am trying to add a Browser Field onto my Main Screen. Currently, I have:

public class BrowserFieldDemo extends UiApplication {

private MainScreen _browserScreen;
private BrowserField _bf2;
private BrowserFieldConfig _bfConfig;

public BrowserFieldDemo(final String url) {
    _browserScreen = new MainScreen();
    _bfConfig = new BrowserFieldConfig();
    _bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,
            BrowserFieldConfig.NAVIGATION_MODE_POINTER);
    _bfConfig.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED,
            Boolean.TRUE);
    _bf2 = new BrowserField(_bfConfig);
    _browserScreen.add(_bf2);

    invokeLater(new Runnable() {
        public void run() {
            _bf2.requestContent(url);
            pushScreen(_browserScreen);
        }
    });
}
}

What is the correct way to insert the browser to into Main Screen class, amongst other UI elements? For example, add(myBrowserField)? I am unsure how to set up the BrowserFieldDemo class to do this, because I need to extend UIApplication for the Thread...

public class LoginScreen extends MainScreen implements FieldChangeListener{}

Thanks!


Update:

I now have the following:

public class LoginBrowserField extends Thread {

    private LoginScreen loginScreen;
    private String url;

    public LoginBrowserField(String url, LoginScreen loginScreen) {
        this.loginScreen = loginScreen;
        this.url = url;
    }

    public void run() {
       synchronized (UiApplication.getEventLock()) {
        loginScreen.changeURL(url);
       }
    }
}

public class LoginScreen extends MainScreen implements FieldChangeListener {

     public void changeURL(final String url) {
        _bf2.requestContent(url);
        _bf2.setFocus();
     }

}

And to start the browser, I call (in LoginScreen):

LoginBrowserField browser = new LoginBrowserField(url, this);
browser.start();

However, I am not sure if my setup above is correct because it is taking a really long time for the browser to appear in the application, and it does not correctly resolve my URL. Can you see anything that is wrong?

Thanks!

+1  A: 

If you need a new thread for running code in the background (i.e. loading data via HTTP) you can just create a new Thread object and start() it - no need to subclass UiApplication for this.

On the other hand, if you need to schedule some code to run in the event handler thread (such as pushScreen), you can use UiApplication.getUiApplication() to get an instance of a UiApplication, for example:

UiApplication.getUiApplication().invokeLater(new Runnable() {
    public void run() {
        pushScreen(_browserScreen);
    }
});
Marc Novakowski
Thanks for your help, Marc! I updated my post above, can you check it out? I did what you suggested, but I'm not sure if I implemented it correctly, as the browser is resolving the page incorrectly and very slowly...Thanks!
behrk2
You may want to check out the Browser Field Demo in the sample apps that ship with the RIM developer tools. It demonstrates how to use the field and how to set up the resource fetching threads.
Marc Novakowski
i worked with this way and it workedThanks!!!!!!!!!!!!
SWATI
A: 

Hi SWATI, Can you please send me the code. i struggele to add a Browser Field onto my Main Screen.

Thanks.

Saravanan