//mobile and embedded /
Working with JSR-211:
Content Handler API
VIKRAM GOYAL
Learn how to use CHAPI, which solves a very specific problem in a clean and
elegant manner.
JAVA IN ACTION
The “Content Handler API (CHAPI),” also known as JSR-
211, is one of those nifty little APIs
that doesn’t seem to do much at
first glance, but the more you look
at it, the more you realize how
useful it really is. It solves a very
specific problem, and it does that
in a clean and elegant manner.
In this article, I will help you
understand the problem domain
this API addresses and what it
does to solve the problem. I will
give you a rundown of the API’s
structure and how best to use it
in your own MIDlets. Finally, I will
show an example of how to use
this API with a simple use case.
Note: The source code that
accompanies this article can be
downloaded here.
ages that they might have captured using their device’s camera.
Instead of using the device’s own
image browser, they can use your
nifty little app, which provides
extra tools (for example, a tool for
sharing images online, perhaps).
When users browse and want
to see an image, you want to be
able to put some copyright information on the image. Instead
of relying on the built-in image
viewing capabilities of the device
or the Java virtual machine (JVM),
you want to make sure that each
image opens in a special image
viewer that displays this copyright information.
In a nutshell, you want to create
a new image content handler that
can always display images with
your copyright. You could write
some tricky code. Or you could
use the Content Handler API
and register your special content handler for images with the
device’s Application Management
Software (AMS).
The Content Handler API
CHAPI provides an execution
model that allows your applications to invoke Java Platform,
Micro Edition (Java ME) and
non-Java applications. What this
means is that you register your
existing content handlers with
this API and the API allows you to
invoke them. You, of course, need
to provide some sort of identification by which a content handler
can be invoked.
For example, let’s say you want
all JPG images to be handled by a
specific image content handler (as
discussed earlier in “The Problem
Domain”). You register
the content handler
class either through an
entry in the manifest
file or, programmatical-
ly, by notifying the AMS
that JPG MIME types
are now to be handled
by this special con-
tent handler. (If there
are multiple handlers
registered, CHAPI will pick one of
them randomly.) Identification
can be done not only by MIME
type (or content type) but also by
the URL or the content handler
ID construct. You can also regis-
ter multiple content handlers for
the same types, and then use the
actions to specify which particular
handler to invoke.
ABOUT US
blog
PHOTOGRAPH BY
JONATHAN WOOD/
GETTY IMAGES
The Problem Domain
Consider the case where you are
creating an application that will
allow budding photographers to
browse the image library within
their device, which contains im-
The Registry and Invocation
The Registry class, as you might
expect, is the central repository
of all known content handlers
within the Java ME environment.
It provides the lifecycle methods
of all content handlers
(namely registration
and un-registration),
providing meta infor-
mation and, of course,
providing the actual
invocation of content
handlers. Each content
handler is marked us-
ing zero or more con-
tent types (for exam-
JSR-211 is one
of those nifty
little APIs that
doesn’t seem
to do much at
first glance.