繁体   English   中英

如何在blackberry中的类中添加addGlobalEventListener?

[英]How to add addGlobalEventListener in a class in blackberry?

我做了一个多入口点项目,其中App2设置为自动运行,App1根据用户请求运行。 我正在尝试从App2调用App1的全局事件。

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));
        }
    }
}

然后在其他课程中我尝试过

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));
}

}

如果我调用该事件三次

Event Recived#counter#2
Event Recived#counter#2
Event Recived#counter#2

而预期的结果是

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

我猜这个App2的对象不是null而是eventOccurred永远不会被调用。 输出清楚地表明,即使在构造函数中添加了globalEventListenercallMethodonclick也无法发布Global事件。

必须是这样的。

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

store.put()抛出一个IllegalArgumentException,因为商店中有一些东西(参见API参考),但是这个异常由一些系统线程处理,它调用eventOccured()方法并且不显示这个异常。 它是各种黑莓虫之一。

您没有更新RichTextField中的文本。 仅当没有与App2.RTSID_MY_APP关联的runtimestorage对象时,它才会添加到屏幕。

您的代码需要通过将其放入App1对象的字段来保持RichTextField的句柄。 然后更新run()方法中的文本。

我编辑了你的代码,在if语句中添加了大括号,这使得这一点更加清晰。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM