ContextListener

From MobiComp

Jump to: navigation, search

Summary

A ContextListener is a MobiComp component that receives notification of ContextEvents (PUT or REMOVE) and, performs some action based on the ContextElement carried by the event object.

They receive event notifications whenever a ContextElement is put into, or removed from, the store. On receiving a notification, the listener may get the element from the ContextEvent and use it as required.

See also,

Developer info

Classes needing to be notified of ContextEvents implement the ContextListener interface. Java source code for this class is found in the common branch of the src tree in the package org.mobicomp.context. Note that this is not a subclass of java.util.EventListener as that class is not present in the CLDC.

The ContextListener interface provides a single event notification method

public abstract void contextEvent ( ContextEvent ev );

which must be implemented by all subclasses.

Listeners should register with the ContextService, indicating the element(s) about which they need to be notified. All event notifications takes place within the same thread as all other ContextService actions. For this reason, it is important to minimise the code executed in the contextEvent method. To register a listener class with the ContextService, include the following call. Usually this placed in the constructor of the listener class.

ContextService.getService().addListener ( ContextListener lsnr, String id, String pred );

The ContextListener parameter is always this, id is the identifier (subject) of the entity of interest, and pred is the name of the predicate of interest. Multiple calls may be used to register for notification of different events. Either or both of the id and pred parameters may be null, in which case they act as wildcards.

The recommended way to write a contextEvent method is:

ContextElement ce = (ContextElement) ev.getElement();
  • if necessary (i.e. when wildcards are used for the id or pred parameters, or the listener has registered for multiple events) extract any parts of the element needed to test whether it is of interest to the listener, e.g.
String pred = ce.getPredicate();
  • test whether the element is of interest, e.g.
if ( "InterestingElement".equals(pred) && && ev.getEventType() == ContextEvent.PUT ) {
  • either
    • set a flag to indicate that an interesting event has ocurred,
    • test for this flag in a separate thread run loop
    • call a method to perform the required action from this run loop.
This approach is recommended where a single listener is required to act on several different events. For an example, see the GPSListener class in the common branch of the src tree in the package org.mobicomp.gps.
  • or
    • include an inline thread in the contextEvent method.
This approach is recommended for simpler listeners which respond only to a single event. For an example, see the BeaconListener class in the common branch of the src tree in the package org.mobicomp.beacon.