Register your application with the BlackBerry Messenger platform

Before you begin: Create a subclass of BBMPlatformApplication.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.bbm.platform.BBMPlatformApplication;
    import net.rim.blackberry.api.bbm.platform.BBMPlatformContext;
    import net.rim.blackberry.api.bbm.platform.BBMPlatformContextListener;
    import net.rim.blackberry.api.bbm.platform.BBMPlatformManager;
    import net.rim.device.api.ui.container.MainScreen;
    import net.rim.device.api.ui.UiApplication;
  2. Create the application framework by extending the UiApplication class. Inside the new class, declare an instance of MyBBMAppPlugin as a member variable of the MyBBMPlatformApp class.
    public class MyBBMPlatformApp extends UiApplication
    {
         private MyBBMAppPlugin myPlugin;   
    }
  3. Create the framework for a custom screen by extending the MainScreen class.
    private class MyBBMPlatformScreen extends MainScreen
    {
        MyBBMPlatformScreen()
        {
           setTitle("My BBM Platform screen");
        }
    }
  4. In the constructor for the MyBBMPlatformApp class, create an instance of your MyBBMAppPlugin class and invoke pushScreen() to display the screen for the application.
    public MyBBMPlatformApp() 
    {
        myPlugin = new MyBBMAppPlugin();
        pushScreen(new MyBBMPlatformScreen());
    }
  5. In the same constructor, call invokeLater(new Runnable()) to add the object to the event queue for your application. In the object's run() method invoke BBMPlatformManager.register() in a try/ctch block to register your plug-in with the BlackBerry Messenger platform. The register() method returns a BBMPlatformContext object for this application.
    invokeLater(new Runnable()
    {
      public void run(
      {
        BBMPlatformContext platformContext = null;
            try 
            {
                platformContext = BBMPlatformManager.register(myPlugin);
            }
    	       catch (ControlledAccessException e) 
            {
                // BBM Social Platform has been disabled
            }
    
  6. Define a context change listener. The code sample in step 7 assumes that your context change listener subclass is named MyBBMPlatformContextChangeListener and that it implements the BBMPlatformContextListener interface.
  7. In the run() method, create a new instance of a BBMPlatformContextListener and invoke BBMPlatformContext.setChangeListener() to assign the listener to the BBMPlatformContext.
            if (platformContext != null) 
            {
                MyBBMPlatformContextListener platformContextListener;
                platformContextListener = new MyBBMPlatformContextListener();
                platformContext.setListener(platformContextListener);
             }
        }
     }
    );
  8. In your MyBBMPlatformApp class, create a main() method. In main() create an instance of the MyBBMPluginApp class and add it to the event thread of your device to start the application and enable it to receive events.
    public static void main(String[] args)
    {
        MyBBMPlatformApp theApp = new MyBBMPlatformApp();
        theApp.enterEventDispatcher();
    }
You have now registered your application with the BlackBerry Messenger platform.

Code sample: Registering your application with the BlackBerry Messenger platform

import net.rim.blackberry.api.bbm.platform.BBMPlatformApplication;
import net.rim.blackberry.api.bbm.platform.BBMPlatformContext;
import net.rim.blackberry.api.bbm.platform.BBMPlatformContextListener;
import net.rim.blackberry.api.bbm.platform.BBMPlatformManager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

public class MyBBMPlatformApp extends UiApplication
{   
    private MyBBMAppPlugin myPlugin;

        public MyBBMPlatformApp()
        {
           myPlugin = new MyBBMAppPlugin();
           pushScreen(new MyBBMPlatformScreen());
           invokeLater(new Runnable()
           {
              public void run()
              {
                 BBMPlatformContext platformContext = null;
                 try 
                 {
                     platformContext = BBMPlatformManager.register(myPlugin);
                 }
                 catch (ControlledAccessException e) 
                 {
                    // The BBM platform has been disabled
                 }
                 if (platformContext != null) 
                 {
                    MyBBMPlatformContextListener platformContextListener;
                    platformContextListener = new MyBBMPlatformContextListener();
                    platformContext.setListener(platformContextListener);
                 }
              }
           }
        );
    }

    private class MyBBMPlatformScreen extends MainScreen
    {
        MyBBMPlatformScreen()
        {
            setTitle("BBM Platform application");
        }
    }

    private class MyBBMAppPlugin extends BBMPlatformApplication
    {
        public MyBBMAppPlugin()
        {
            super( "Insert your UUID here" );
        }
    }

    private class MyBBMPlatformContextListener extends BBMPlatformContextListener
    {
        public void accessChanged(boolean isAccessAllowed, int accessErrorCode)
        {
            if (!isAccessAllowed)
            {
                // You cannot access the BBM platform
            }
        }

        public void appInvoked(int reason, Object param)
        {
            // Code for handling different contexts for invocation
        }
    }
	public static void main(String[] args)
    {
        MyBBMPlatformApp theApp = new MyBBMPlatformApp();
        theApp.enterEventDispatcher();
    }
}
Back To Top

Was this information helpful? Send us your comments.