How to add addGlobalEventListener to a class in blackberry?

I made a project with several inputs, where App2 is configured to autostart, and App1 starts at the request of the user. I am trying to trigger 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 another class I tried how

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 trigger the event three times

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

and expected result

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

which, it seems to me, suggests that the object for App2 is not in null, and eventOccurred is never called. the output clearly indicates that callMethodonclickit cannot dispatch a global event, even if it is added in the constructor globalEventListener.

+3
source share
2 answers

It should be like that.

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

store.put() IllegalArgumentException, - (. API), - , eventOccured() . Blackberry.

+1

RichTextField. , runtimestorage, App2.RTSID_MY_APP.

RichTextField, App1. run().

, if, .

0

Source: https://habr.com/ru/post/1769517/


All Articles