Respond to a contact's changes in your contact list

Before you begin: Make sure that you have completed the task, Register your application with the BlackBerry Messenger platform, and that the class that displays the screen passes in a reference to the application's associated BBMPlatformContext object.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.bbm.platform.*;
    import net.rim.blackberry.api.bbm.platform.service.*;
    import net.rim.device.api.ui.component.*;
  2. Create a class that implements the PresenceListener interface and extends MainScreen. In the constructor for this screen class, pass in the application's associated BBMPlatformContext object.
    public class MyBBMContactListScreen implements PresenceListener extends MainScreen
    {
        public MyBBMContactListScreen(BBMPlatformContext platformContext) 
        {
    
        }
    }
  3. In the constructor, invoke BBMPlatformContext.getContactListService().setPresenceListener() to assign the PresenceListener to your application.
    public class MyBBMContactListScreen implements PresenceListener
    {
        public MyBBMContactListScreen(BBMPlatformContext platformContext) 
        {
            platformContext.getContactListService().setPresenceListener(this);
        }
    }
  4. In the MyBBMContactListScreen class, implement the PresenceListener.presenceUpdated() method. The following code displays a dialog box that informs the user of changes to a contact in your BlackBerry Messenger contact list. The following code sample uses a helper method, getEventName(), to parse the eventType parameter and return a readable String.
    public void presenceUpdated(BBMPlatformContact contact, int eventType)
    {
        final String str = "[" + getEventName(eventType) + "] was changed by " + contact.getDisplayName();
        Dialog.inform(str);
    }
    
    private String getEventName(int eventType)
    {
        String eventName;
    
        switch(eventType)
        {
            case PresenceListener.EVENT_TYPE_DISPLAY_PICTURE:
                eventName = "Display picture";
                break;
            
            case PresenceListener.EVENT_TYPE_DISPLAY_NAME:
                eventName = "Display name";
                break;
            
            case PresenceListener.EVENT_TYPE_PERSONAL_MESSAGE:
                eventName = "Personal Message";
                break;
            
            case PresenceListener.EVENT_TYPE_STATUS:
                eventName = "Status";
                break;
            
            default:
                eventName = "Default";
        }
    }

Code sample: Responding to a contact's changes in your contact list

The following code sample assumes that the class that pushed the MyBBMContactListScreen has access to the BBMPlatformContext object for this application and has passed it into the MyBBMContactListScreen constructor.

import net.rim.blackberry.api.bbm.platform.*;
import net.rim.device.api.ui.component.*;

public class MyBBMContactListScreen implements PresenceListener
{
    public MyClass(BBMPlatformContext platformContext) 
    {
       platformContext.getContactListService().setPresenceListener(this);
    }

    public void presenceUpdated(BBMPlatformContact contact, int eventType)
    {
        final String str = "[" + getEventName(eventType) + "] was changed by " + contact.getDisplayName();
        Dialog.inform(str);
    }
 
    private String getEventName(int eventType)
    {
        String eventName;

        switch(eventType)
        {
            case PresenceListener.EVENT_TYPE_DISPLAY_PICTURE:
                eventName = "Display picture";
                break;
        
            case PresenceListener.EVENT_TYPE_DISPLAY_NAME:
                eventName = "Display name";
                break;
        
            case PresenceListener.EVENT_TYPE_PERSONAL_MESSAGE:
                eventName = "Personal Message";
                break;
        
            case PresenceListener.EVENT_TYPE_STATUS:
                eventName = "Status";
                break;
        
            default:
                eventName = "Default";
        }
    }
}
Back To Top

Was this information helpful? Send us your comments.