07-31-2009 03:01 AM
Hi, I have simple (should be) requirements for an app with a Listfield on the MainScreen that displays info extracted from emails.
when the app starts up I need to scan the inbox so I can update the Mainscreen with new data.
THE PROBLEM: My messages are bigger than 2kb so I need to use the MessageListener. In the Changed() event handler for the MessageListener I need to refresh my ListField. In my Main() I use UIApplication.getUIApplication.invokeandwait(...) to run my code.
It doesn't work. My code in Run()... blows up because I and access the UI controls from whatever thread/state/whatever my code is in.
This must be a common pattern. Starting a listener at startup with a callback that executes and has to update the MainScreen (some UI?)
Are there samples, docs etc etc ANYTHING that would show how this should be done?
07-31-2009 04:02 AM
07-31-2009 04:47 AM
07-31-2009 08:44 AM
Hi, I did use the invokeAndWait() but I'm still getting exceptions accessing the ListField control (invalid context or the like).
The issue is I'm calling the the code which attaches the MessageListener from the Main() method. I read somewhere that this code gets
executed in a separate process from the application not just a separate thread?
This is a simple scenario to test. Implement MessageListener on the UIApplication subclass. Attach the listener in the Main() of the app
and in the changed() (or other callback) try to update a TextField on the application's MainScreen.
07-31-2009 08:53 AM
07-31-2009 10:25 AM
I believe you are correct. Main is static and therefore not part of the actual UiApplication, calling UiApplication from within main should not work as your app has not been created yet. You can create a method in your calls and then call it before you enter the event dispatcher.
ie
class MyClass {
myClass(){ //intialize }
void invokeListener(){ //do work }
public void main(String [] args) {
MyClass mc = new MyClass();
mc.invokeListener();
mc.enterEventDispatcher();
}
}
07-31-2009 10:39 AM - last edited on 07-31-2009 10:40 AM
Hi,
That's what I did and it does not work.
I see simplified code snippet below.
MyUIApp extends UIApplication implements MessageListener
{
static MyUIApp objMyUIAppSingleton;
MyScreen objMyScreen;
public MyUIApp() {
objMyScreen = new MyScreen();
pushScreen(objMyScreen);
}
public static void main(String[] args) {
// now process the inbox and if we find a multipart message we have to add
// "this" as the MessageListener
Message objSomeMessage = // some multipart emailmessage
objSomeMessage.addListener(this);
objMyUIAppSingleton.enterEventDispatcher();
}
// MessageListener Event Handler
void changed(...) {
// in here I update my MainScreen
UIApplication.getUIApplication().invokeAndWait(new runnable() {
void run() {
objMyUIAppSingleton.objMyScreen.objTextField.setTe
}
}
class MyUIApp extends MainScreen {
TextField objTextField;
public MyUIApp() {
objTextField = new TextField("mylabel","blank");
// ... standard code - add control to the screen
}
}
}
07-31-2009 10:48 AM
Hi,
The problem is that the event handler on MAIN gets called when the rest of the multi-part email arrives which can/will be after the UIApplication has started. The Event handler in Main needs to notify the UIApplication to refresh because the shared data in GlobalResource has been modified.
I can't sit and wait for the Multi-part emails to arrive before launching the app since I can't control when that will finish.
Is there some way to do some work AUTOMATICALLY once the Screen is loaded and the UIApp is executing? Maybe I can spin up a thread that polls the data to see if it's changed (simplified for discussion) by the MessageListener code?
Thanks
07-31-2009 10:52 AM
07-31-2009 11:48 AM