Display a list of contacts who have installed your application

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 BBMPlatformContext object associated with your application.
  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.container.*;
    import java.util.*;
  2. Create a class that extends MainScreen. In the constructor for this screen class, pass in the application's associated BBMPlatformContext.
    public class MyBBMContactListScreen extends MainScreen
    {
        public MyBBMContactListScreen(BBMPlatformContext platformContext) 
        {
    
        }
    }
  3. In the constructor, invoke BBMPlatformContext.getContactListService() to retrieve a reference to the ContactListService object that is associated with the application.
    ContactListService contactListService = platformContext.getContactListService();
  4. In the constructor, invoke ContactListService.getContacts() to retrieve a BBMPlatformContactList object that contains all of the contacts who have installed this application.
    BBMPlatformContactList contacts = contactListService.getContactList();
  5. In the constructor, invoke BBMPlatformContactList.getAll() to retrieve an enumeration object of all of the contacts in the BBMPlatformContactList.
    Enumeration contactsEnum = contacts.getAll();
  6. In the constructor, you can now traverse the enumeration. The following code sample demonstrates how to display the names of the contacts.
    while(contactsEnum.hasMoreElements()) 
    {
        BBMPlatformContact contact = (BBMPlatformContact)contactsEnum.nextElement();
        myScreen.add(new LabelField(contact.getDisplayName());
    }

Code sample: Displaying a list of contacts who have installed your application

The following code sample assumes that the class that displayed the MyBBMContactListScreen has passed a reference to the application's associated BBMPlatformContext object into the MyBBMContactListScreen constructor.

import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.service.*;
import net.rim.device.api.ui.container.*;
import java.util.*;

public class MyBBMContactListScreen extends MainScreen 
{    
    public MyBBMContactListScreen(BBMPlatformContext platformContext) 
    {
        ContactListService contactListService = platformContext.getContactListService();
        BBMPlatformContactList contacts = contactListService.getContactList();
        Enumeration contactsEnum = contacts.getAll();

        while(contactsEnum.hasMoreElements())
        { 
            BBMPlatform contact = (BBMPlatformContact)contactsEnum.nextElement();
            add(new LabelField(contact.getDisplayName());
        }
    }
}
    

Back To Top

Was this information helpful? Send us your comments.