0

我正在制作一个调用 web 服务异步的小型应用程序,我想在响应完成时触发加载完成事件以便能够移动到下一个表单

我怎样才能以一种好的方式做到这一点?

4

3 回答 3

2

Event firing in Java requires the following elements

  • An event class to transmit information about the event
  • An interface that describes how the bean will notify interested parties, known as listeners A data structure to track listeners
  • Registration methods to add and remove listeners
  • Code to fire the events

The event class will extend java.util.EventObject like this:

public class MyEventClass extends java.util.EventObject {
         //here's the constructor
         public MyEventClass(Object source) {
             super(source);
         }
}

The interface will look something like this:

 public interface MyEventClassListener {
     public void handleMyEventClassEvent(EventObject e);
}

The event source will look something like this:

public class MyEventSource {
  private List _listeners = new ArrayList();
  public synchronized void addEventListener(MyEventClassListener listener){
     _listeners.add(listener);
  }
  public synchronized void removeEventListener(MyEventClassListener listener){
    _listeners.remove(listener);
  }
  // call this method whenever you want to notify
  //the event listeners of the particular event
  private synchronized void fireEvent(){
    MyEventClass event = new MyEventClass(this);
    Iterator i = _listeners.iterator();
    while(i.hasNext()){
      ((MyEventClassListener) i.next()).handleMyEventClassEvent(event);
    }
  }
}

The event listener will look something like this:

public class MyEventListener implements MyEventClassListener {
  // ... code here

  //implement the required method(s) of the interface
  public void handleMyEventClassEvent(EventObject e) {
    // handle the event any way you see fit
  }
}

You’ll need to register the MyEventListener object with the MyEventSource object by call its addEventListener method.

Good luck, be free to contact me for more details.

于 2012-09-24T10:27:13.707 回答
1

要在J2ME中创建事件,请尝试以下代码:

EventList eventList = (EventList)PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE);

Event event = eventList.createEvent();
...
event.addString(Event.SUMMARY, PIMItem.ATTR_NONE, summary);
...
event.commit();

有关更多详细信息,请参阅JSR 75

于 2012-09-24T10:14:43.220 回答
0

我已经通过创建一个接口并将其提供给我的方法解决了这个问题,该接口更改标志布尔值并调用另一个方法,告诉我它已经完成工作,所以我可以继续我的工作并且它正在运行另一个线程

于 2013-01-28T14:48:37.237 回答