Table of ContentsCommunicationsConclusion

Push Registry

The MIDP 2 push registry is a cool new addition for game developers. Basically, it lets you wake up a MIDlet when a timer event goes off or when incoming traffic has arrived. This is really useful if you have a game that periodically updates data, or if you need to react when another party has done something (such as making a move in a chess game). All you need to do is register your MIDlet with the Application Management System (AMS), and it'll call you back when something happens.

To have the AMS listen for events, you need to register your MIDlet with it. You can do this statically, via JAD attributes, or dynamically, using the javax.microedition.io.PushRegistry API.

To register statically, add an attribute to the JAD file for your MIDlet in the following form:

MIDlet-Push-<n>: <ConnectionURL>, <MIDletClassName>, <AllowedSender>

Specify the connection as the URL of the incoming connection (basically the same as the connection URL you would use in your application). The MIDlet class name is the name of the class you want woken up when there's traffic, and the filter is a protocol-specific filter for the events to which you want to listen. Here's an example:

MIDlet-Push-1: socket://:79, com.your.SocketMidlet, 192.168.0.*
MIDlet-Push-2: sms://:500, com.your.SMSMidlet, *

You also register a connection from within your MIDlet using the API method

registerConnection.

PushRegistry.registerConnection("socket://:79", this.getClass().getName(),
"192.168.0.*");
PushRegistry.registerConnection("sms://:500", this.getClass().getName(), "*");

Using the API, you also register for a wakeup call using the registerAlarm method. The AMS will trigger your MIDlet at the set time.

PushRegistry.registerAlarm(this.getClass().getName(), (new java.util.Date().getTime()) +
(60 * 10 * 1000));

NOTE

Tip

When you attempt to register an application with the push registry, the MID might ask permission from the user. If the user denies this option, your application will throw a SecurityException. Your MIDlet should expect this and deal with it appropriately.

If you later want to remove any of these registrations, you can use the unregisterConnection method. Static registrations are automatically removed when the MIDlet is uninstalled.

When a push registry event occurs, your application's startApp method is called. If you're wondering what method is called when your MIDlet is already running, the answer is none. The assumption is that if your MIDlet is running, you'll deal with any events in your own code. The push registry is only there for waking up MIDlets that are not already running.

To figure out why you were woken up, you can query the push registry's list of the connections ready for your application by using the listConnections method, which will return an array of strings using the connection URL format. Using this string, you can open a socket and read in the waiting data.

    Table of ContentsCommunicationsConclusion