The Java
VM supports multitasking from JP-7 and above. Several MIDlets may run minimized "in the background" concurrently. In order to be able to alert the user of events a framework for posting alerts to the Activity Menu was introduced in late JP-7 phones. This example shows how a minimized MIDlet can run as a background application and alert a user with vibration, sound, light and posting an event to the Activity Menu.
Download example MIDlet>>
One of the issues with executing in the background might be to alert a user when the MIDlet needs attention. A common situation might be when the MIDlet is finished with a lengthy computation or when an external event has taken place, like the reception of a new message or e-mail. There are five types of events that can help the MIDlet to attract attention from the user.
- Playing sound
- Vibrating
- Lighting up the screen
- Maximizing the MIDlet
- Posting an event to the Activity Menu
The last one is a persistent alert in comparison to the others in the sense that the user will notice that the event has occurred long after the actual event has taken place. This can be compared to the notification of a missed phone call.

This feature was introduced in late JP-7 phones and will not work on earlier phones. To check for compatibility make sure that System.getProperty("com.sonyericsson.java.platform"); returns JP-7.5 or higher.
float javaPlatform = Float.parseFloat(System.getProperty("com.sonyericsson.java.platform").substring(3));
if(javaPlatform < 7.5){
System.out.println("Uncompatible platform!");
destroyApp(true);
}
In order to use the notification functionality in JP-7 and onwards one has to define one class for posting events and one interface for receiving callbacks when the event is selected.
public class UIActivityMenu {
public static synchronized native UIActivityMenu getInstance(MIDlet midlet);
public native void setEventListener(UIEventListener eventListener);
public native int addEvent(String title, Image titleIcon);
public native int addEvent(String title, String desc, Image titleIcon, Image descIcon);
public native int addEvent(String title, String desc, int titleNumberText, Image titleIcon, Image descIcon);
}
public interface UIEventListener {
public abstract void eventAction(int eventId);
}
To handle an event a new event listener must be defined.
UIActivityMenu.getInstance(this).setEventListener(new UIEventListener() {
public void eventAction(int id) {
//Handle the event...
eventTimer.cancel();
}
});
To post an event simply call add event on the instance of the activity menu.
UIActivityMenu.getInstance(ActivityMenuEvents.this).addEvent(
"ActivityMenuEvents",
"Hi! I'm an event",
null,
null);
Posting to the activity menu puts a new event to the "New events" list but this might not be enough to attract immediate user attention. To try to attract immediate attention one might want to play sound, vibrate and light up / flash the screen. Playing sound while being minimized works fine but using the Nokia UI extension API to vibrate and light up the screen has no effect. Instead one can use the support for iMelody introduced in JSR-135 in JP-2. The iMelody format has support for sound, vibration and lighting. A typical example of an iMelofy file (.imy) can look like this.
BEGIN:IMELODY
VERSION:1.2
FORMAT:CLASS1.0
BEAT:400
STYLE:S1
VOLUME:V15
MELODY:backonvibeon*4c2backoffvibeoffr2backonvibeon*4c3backoffvibeoffr3backonvibeon*
4c3backoffvibeoffr3backonvibeon*5c1r1r1vibeoff
END:IMELODY
To play the iMelody file embedded as a resource one simply uses the JSR-135 player.
InputStream is = getClass().getResourceAsStream("/alert.imy");
Player player = Manager.createPlayer(is, "audio/iMelody");
player.realize();
player.start();
More information: