Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Global Events and Global Event Listeners

by BlackBerry Development Advisor ‎02-16-2010 10:27 AM - edited ‎02-18-2010 09:18 AM (7,932 Views)

Summary


This article applies to the following:

  • BlackBerry Wireless Handhelds™ based on Java™


Details


A BlackBerry application can notify and communicate with other applications through the use of GlobalEvents.


GlobalEvents are delivered to every application on the BlackBerry that is implementing a GlobalEventListener. When firing a GlobalEvent, you specify a unique ID that allows the listening application to determine if it is an event that it should act on or ignore. A GlobalEvent can also be used to transport data to the listening application. Not only can you alert the listening application that it needs to do some work, you can send it data it can process. A GlobalEvent can contain two integer variables as well as two objects that are delivered to the listening application.


The sample applications below demonstrate how a GlobalEvent can be used as a mechanism for application-to-application communication. The GlobalEventFiring application fires a GlobalEvent that is received by the GlobalEventListening application. The GlobalEventListening application then fires another acknowledging GlobalEvent that is received by the GlobalEventFiring application, causing a Dialog alert to be displayed. In actual practice it would not be required to acknowledge that a GlobalEvent was received: it is just provided for demonstration purposes.


GlobalEventFiring.java



/*
* GlobalEventFiring
*
* Fires a GlobalEvent and listens for acknowledgement.
*/

package com.samples.eventFiring;

import net.rim.blackberry.api.mail.*;
import net.rim.blackberry.api.mail.event.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

class GlobalEventFiring extends UiApplication implements GlobalEventListener
{
private MainScreen mainScreen;

public static void main(String args[])
{
GlobalEventFiring theApp = new GlobalEventFiring();
theApp.enterEventDispatcher();
}

public GlobalEventFiring()
{

mainScreen = new MainScreen();

//Add this class as a global event listener.
addGlobalEventListener(this);

mainScreen.setTitle(new LabelField("Global Event Firing"));

MenuItem fireEvent = new MenuItem("Fire Global Event", 20, 20)
{
public void run()
{
//Post the GlobalEvent.
//Long = com.samples.globalEventFiring.GlobalEventFiring
ApplicationManager.getApplicationManager().postGlobalEvent(0xba4b84944bb7429eL);
}
};

mainScreen.addMenuItem(fireEvent);
pushScreen(mainScreen);

}

//This method is invoked after a GlobalEvent is fired.
//This sample does not make use of passing any integer or Object data with the GlobalEvent,
//but they can be used to convey information between applications.
public void eventOccurred( long guid, int data0, int data1, Object object0, Object object1)
{
//Is this the GlobalEvent we are waiting for?
//Long = com.samples.globalEventFiring.GlobalEventListening
if (guid == 0x7d3a74a5ccfe6483L)
{
//Yes it is.

System.out.println("Acknowledgement received.");

UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Event was fired and acknowledged.");
}
});
}
}
}


GlobalEventListening.java



/*
* GlobalEventListening
*
* Listens for a GlobalEvent and fires back an acknowledgement.
* This application must be set to auto run at startup.
*/

package com.samples.eventFiring;

import net.rim.blackberry.api.mail.*;
import net.rim.blackberry.api.mail.event.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

class GlobalEventListening extends UiApplication implements GlobalEventListener
{
private MainScreen mainScreen;

public static void main(String args[])
{
GlobalEventListening theApp = new GlobalEventListening();
theApp.enterEventDispatcher();
}

public GlobalEventListening()
{
//Add this class as a global event listener.
addGlobalEventListener(this);
}
//This method is invoked after a GlobalEvent is fired.
//This sample does not make use of passing any integer or Object data with the GlobalEvent,
//but they can be used to convey information between applications.
public void eventOccurred( long guid, int data0, int data1, Object object0, Object object1)
{
//Is this the GlobalEvent we are waiting for?
//Long = com.samples.globalEventFiring.GlobalEventFiring
if (guid == 0xba4b84944bb7429eL)
{
//Yes it is.
System.out.println("Event received, sending acknowledgment...");

//Fire another GlobalEvent to acknowledge to the originating class that
//it was received. This process is not required, only included for
//demonstration purposes.
//Long = com.samples.globalEventFiring.GlobalEventListening
ApplicationManager.getApplicationManager().postGlobalEvent(0x7d3a74a5ccfe6483L);
}
}

}




Contributors
Users Online
Currently online: 21 members 843 guests
Please welcome our newest community members: