Display content on a phone screen

In the following code sample, custom content is added to the incoming call screen by overriding AbstractPhoneListener.callIncoming(). You can display content on other call screens by overriding callWaiting(), callInitiated(), and callAnswered().

Before you begin: Make sure that the following sample application runs in the background when the BlackBerry® device starts. In the BlackBerry® Java® Plug-in for Eclipse®, change the BlackBerry application descriptor for the sample application. For more information, see the BlackBerry Java Plug-in for Eclipse Development Guide.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.phone.*;
    import net.rim.blackberry.api.phone.phonegui.*;
    import net.rim.device.api.system.*;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
  2. Create the application framework by extending the Application class. In the constructor, invoke Phone.addPhoneListener() to register the listener that is created in step 3. In main(), invoke enterEventDispatcher() to enable the application to receive events.
    public final class MyPhoneScreen extends Application
    {
        public MyPhoneScreen()
        {
            Phone.addPhoneListener(new MyPhoneScreenContent());
        }
        public static void main(String[] args)
        {
            new MyPhoneScreen().enterEventDispatcher();
        }
    }
  3. Create a class that extends the AbstractPhoneListener class. Create a constructor for this new class.
    final class MyPhoneScreenContent extends AbstractPhoneListener
    {
        public MyPhoneScreenContent()
        {
        }
    }
  4. In the class that extendsAbstractPhoneListener, override AbstractPhoneListener.callIncoming(). Create an instance of the ScreenModel class. Obtain an instance of the incoming call screen by invoking ScreenModel.getPhoneScreen(). Pass in parameters to specify the call screen orientation and type that you want to obtain. In the following code sample, the portrait orientation of the incoming call screen is obtained. Because devices with touch screens support portrait and landscape orientations, you pass in the parameter PhoneScreen.LANDSCAPE to obtain the landscape orientation of the incoming call screen.
    public void callIncoming(int callId)
    {
        ScreenModel screenModel = new ScreenModel(callId);
    
        PhoneScreen phoneScreenPortrait = screenModel.getPhoneScreen(PhoneScreen.PORTRAIT, PhoneScreen.INCOMING);
    }
  5. In callIncoming(), create custom content to add to the call screen. In the following code sample, text is added to the incoming call screen by using label fields. Override LabelField.paint() to change the color of the label field.
    LabelField labelField1 = new LabelField("Hello") 
    {
        public void paint(Graphics g) 
        {
            g.setColor(Color.GREEN);
            super.paint(g);
        }
    };    
    LabelField labelField2 = new LabelField(" to the World.")
    {
        public void paint(Graphics g)
        {
            g.setColor(Color.RED);
            super.paint(g);
        }
    };
  6. In callIncoming(), specify the font for the custom content. In the following code sample, the font from the call screen is used. Invoke PhoneScreen.getCallerInfoFont() to obtain the font that is used by the call screen. Invoke Field.setFont() and pass in the call screen font to specify the font for the label fields that were created in step 5. Invoke PhoneScreen.add() to add the label fields to the call screen.
    labelField1.setFont(phoneScreenPortrait.getCallerInfoFont());
    labelField2.setFont(phoneScreenPortrait.getCallerInfoFont());
    
    phoneScreenPortrait.add(labelField1);
    phoneScreenPortrait.add(labelField2);
  7. In callIncoming(), invoke ScreenModel.sendAllDataToScreen() to add the custom content to the incoming call screen.
    screenModel.sendAllDataToScreen();

Code sample: Displaying content on a phone screen

import net.rim.blackberry.api.phone.*;
import net.rim.blackberry.api.phone.phonegui.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;

public final class MyPhoneScreen extends Application
{
    public MyPhoneScreen()
    {
        Phone.addPhoneListener(new MyPhoneScreenContent());
    }
    public static void main(String[] args)
    {
        new MyPhoneScreen().enterEventDispatcher();
    }
}
final class MyPhoneScreenContent extends AbstractPhoneListener
{
    public MyPhoneScreenContent()
    {

    }
    public void callIncoming(int callId)
    {
        ScreenModel screenModel = new ScreenModel(callId);
        PhoneScreen phoneScreenPortrait = screenModel.getPhoneScreen(PhoneScreen.PORTRAIT, PhoneScreen.INCOMING);

        LabelField labelField1 = new LabelField("Hello")
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.GREEN);
                super.paint(g);
            }
        };    
        LabelField labelField2 = new LabelField(" to the World.")
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.RED);
                super.paint(g);
            }
        };

        labelField1.setFont(phoneScreenPortrait.getCallerInfoFont());
        labelField2.setFont(phoneScreenPortrait.getCallerInfoFont());

        phoneScreenPortrait.add(labelField1);
        phoneScreenPortrait.add(labelField2); 

        screenModel.sendAllDataToScreen();
    }
}

Was this information helpful? Send us your comments.