开发者

How to add addGlobalEventListener in a class in blackberry?

开发者 https://www.devze.com 2023-01-19 19:38 出处:网络
I have made a multiple entry-point project, where App2 is set to autorun and App1 runs on user request.

I have made a multiple entry-point project, where App2 is set to autorun and App1 runs on user request. I am trying to invoke a global event from App1, received by App2.

public class App2 implements GlobalEventListener {
    static public int counter = 0;
    public static final long countId = 0x1251402f595f81a5L;
    public static final long eventId = 0xba4b84944bb7429eL;

    private App2() {
        Application.getApplication().addGlobalEventListener(this);
    }

    public static App2 waitForSingleton() {
        counter = 2; // Added the counter in Runtime store in a similar way as
        // added in eventOccured method
        // Deleted some unuseful code
    }

    public void eventOccurred(long guid, int data0, int data1, Object object0,
            Object object1) {
        if (guid == eventId) {
            callMethodOnOccuranceOfEvent();
        }
    }

    public void callMethodOnOccuranceOfEvent() {
        counter++;
        RuntimeStore store = RuntimeStore.getRuntimeStore();
        Object obj = store.get(countId);
        if (obj == null) {
            store.put(countId, new Integer(counter));
        } else {
            store.put(countId, new Integer(counter));
        }
    }
}

Then in other class I tried like

public class App1 extends MainScreen {
public App1() {
}

protected void makeMenu(Menu menu, int instance) {
    super.makeMenu(menu, instance);
    menu.add(new MenuItem("Call", 20, 10) {
        public void run() {
            callMethodonclick();
        }
    });
}

public void callMethodonclick() {
    ApplicationManager.getApplicationManager().postGlobalEvent(App2.eventId);
    RuntimeStore store = RuntimeStore.getRuntimeStore();
    Integer c = (Integer) store.get(App2.countId);
    add(new RichTextField("Event Recived#counter#" + c));
}

}

If I invoke the event for three times

Eve开发者_C百科nt Recived#counter#2
Event Recived#counter#2
Event Recived#counter#2

while the Expected result is

Event Recived#counter#3
Event Recived#counter#4
Event Recived#counter#5

which I guess suggests that object for App2 in not null but eventOccurred never invoked. the output clearly suggests that callMethodonclick is not able to post Global event,even though globalEventListener was added in constructor.


Must be like this.

if (obj == null) {
     store.put(countId, new Integer(counter));
} else {
     store.replace(countId, new Integer(counter));
}

store.put() throw an IllegalArgumentException, because there is something in a store(see API Reference), but this Exception is handled by some system thread, that invokes eventOccured() method and show nothing about this Exception. It is one of the various Blackberry bug.


You are not updating the text in the RichTextField. It only gets added to the screen when there is no runtimestorage object associated with App2.RTSID_MY_APP.

Your code needs to keep a handle on the RichTextField by putting it into a field of the App1 object. Then update the text in the run() method.

I edited your code, adding braces to the if statement, which makes this more clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消