User profile box
The
UserProfile class contains an enumerated set of
UserProfileBoxItem objects. Each
UserProfileBoxItem has an ID (a primary key), an icon,
and some text. A
UserProfileBoxItem can represent anything from a display picture
and character name within your game, to a badge signifying an accomplishment.
A user profile box is a list of items that are associated with the user
and your application that the user can share with their
BlackBerry
Messenger contacts.
You can use the
UserProfileBox class to add, remove, update, or retrieve
the
UserProfileBox object's associated
UserProfileBoxItems.
Display the contents of the user's profile box
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
MyUserProfileScreen screen passes in a reference to the
BBMPlatformContext object associated with the
application.
-
Import the required classes and interfaces.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.util.*;
import java.util.*;
-
Create a class that extends
MainScreen. In the constructor for this screen
class, pass in the application's associated
BBMPlatformContext object.
public class MyUserProfileScreen extends MainScreen
{
public MyUserProfileScreen(BBMPlatformContext platformContext)
{
}
}
-
Invoke
BBMPlatformContext.getUserProfile() to retrieve a
reference to the
UserProfile object that is associated with this
BBMPlatformContext. This instance is the current
user's profile.
UserProfile userProfile = platformContext.getUserProfile();
UserProfileBox userProfileBox = userProfile.getProfileBox();
-
Invoke
userProfileBox.isAccessible() in an
if statement to make sure that the profile box is accessible.
if (userProfileBox.isAccessible())
{
}
-
Invoke
UserProfileBox.getItems() to retrieve
all of the items in the profile box.
if (userProfileBox.isAccessible())
{
UserProfileBoxItem[] items = userProfileBox.getItems();
}
-
Iterate through the
UserProfileBoxItem[]. For each UserProfileBoxItem, invoke
Screen.add() to display a
BitmapField and a
LabelField containing the
UserProfileItem object's icon and text.
if (userProfileBox.isAccessible())
{
UserProfileBoxItem[] items = userProfileBox.getItems();
for(int i = 0; i < items.length; i++) {
UserProfileBoxItem profileItem = items[i]
Bitmap icon = userProfileBox.getIcon(profileItem.getIconId());
myScreen.add(new BitmapField(icon));
myScreen.add(new LabelField(profileItem.getText()));
}
}
Code sample: Displaying the contents of a user's profile box
The following code sample assumes that the class that displayed the
MyUserProfileBoxScreen has passed a reference to the
application's associated
BBMPlatformContext object into the
MyUserProfileBoxScreen constructor.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.util.*;
import java.util.*;
public class MyUserProfileBoxScreen extends MainScreen
{
public MyUserProfileBoxScreen(BBMPlatformContext platformContext)
{
UserProfile userProfile = platformContext.getUserProfile();
UserProfileBox userProfileBox = userProfile.getProfileBox();
if (userProfileBox.isAccessible())
{
UserProfileBoxItem[] items = userProfileBox.getItems();
for(int i = 0; i < items.length; i++) {
UserProfileBoxItem profileItem = items[i]
Bitmap icon = userProfileBox.getIcon(profileItem.getIconId());
myScreen.add(new BitmapField(icon));
myScreen.add(new LabelField(profileItem.getText()));
}
}
}
}
Add an item to the user's profile box
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
MyUserProfileBoxScreen screen passes in a reference to
the
BBMPlatformContext object that is associated with the
application.
-
Import the required classes and interfaces.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.*;
import java.util.*;
-
Create a class that extends
MainScreen. In the constructor for this screen
class, pass in the application's associated
BBMPlatformContext object.
public class MyUserProfileBoxScreen extends MainScreen
{
public MyUserProfileBoxScreen(BBMPlatformContext platformContext)
{
}
}
-
In the contructor, invoke
BBMPlatformContext.getUserProfile() to retrieve a
reference to the
UserProfile object that is associated with this
BBMPlatformContext. The returned instance is the
current user's profile.
UserProfile userProfile = platformContext.getUserProfile();
-
Invoke
UserProfile.getProfileBox() to retrieve a reference
to the
UserProfileBox object that is associated with this
UserProfile.
UserProfileBox profileBox = userProfile.getProfileBox();
if (profileBox.isAccessible())
{
}
-
Register icons to be used in the profile box by invoking profileBox.registerIcon() for each icon. Invoke
EncodedImage.getEncodedImageResource() to create an
image object to use as the icon.
if (profileBox.isAccessible())
{
UserProfileBox profileBox = userProfile.getProfileBox();
int iconId = 1;
EncodedImage icon = EncodedImage.getEncodedImageResource("myImg.jpg");
profileBox.registerIcon(iconId, icon);
}
-
Invoke
UserProfileBox.addItem() passing in the icon ID that
you just registered and a
String to add an item to the profile box.
if (profileBox.isAccessible())
{
UserProfileBox profileBox = userProfile.getProfileBox();
int iconId = 1;
EncodedImage icon = EncodedImage.getEncodedImageResource("myImg.jpg");
profileBox.registerIcon(iconId, icon);
profileBox.addItem(iconId, "My message.");
}
Code sample: Adding an item to the user's profile box
The following code sample assumes that the class that displayed the
MyUserProfileBoxScreen has passed a reference to the
application's associated
BBMPlatformContext object into the
MyUserProfileBoxScreen constructor.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.*;
import java.util.*;
public class MyUserProfileBoxScreen extends MainScreen
{
public MyUserProfileBoxScreen(BBMPlatformContext platformContext)
{
UserProfile userProfile = platformContext.getUserProfile();
UserProfileBox profileBox = userProfile.getProfileBox();
if (profileBox.isAccessible())
{
UserProfileBox profileBox = userProfile.getProfileBox();
int iconId = 1;
EncodedImage icon = EncodedImage.getEncodedImageResource("myImg.jpg");
profileBox.registerIcon(iconId, icon);
profileBox.addItem(iconId, "My message.");
}
}
}
Remove an item from the user's profile box
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
MyUserProfileBoxScreen screen passes in a reference to
the
BBMPlatformContext object that is associated with the
application.
-
Import the required classes and interfaces.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.device.api.system.*;
net.rim.device.api.ui.component.*;
import java.util.*;
-
Create a class that extends
MainScreen. In the constructor for this screen
class, pass in the application's associated
BBMPlatformContext object.
public class MyUserProfileBoxScreen extends MainScreen
{
public MyUserProfileBoxScreen(BBMPlatformContext platformContext)
{
}
}
-
In the constructor, invoke
BBMPlatformContext.getUserProfile() to retrieve a
reference to the
UserProfile object associated with this
BBMPlatformContext. This instance is the current
user's profile.
UserProfile userProfile = platformContext.getUserProfile();
-
Invoke
UserProfile.getProfileBox() to retrieve a reference
to the
UserProfileBox object that is asssociated with this
UserProfile.
UserProfileBox profileBox = userProfile.getProfileBox();
if (profileBox.isAccessible())
{
}
-
Invoke
UserProfileBox.removeItem() passing in the ID of the
item to remove from the profile box. This example removes the first item contained in the UserProfileBoxItem[]
returned by
UserProfileBox.getItems().
UserProfileBox profileBox = userProfile.getProfileBox();
if (profileBox.isAccessible())
{
int[] itemIds = profileBox.getItemIds();
if(itemIds.length >= 1) {
profileBox.removeItem(itemIds[0]);
}
}
Code sample: Removing an item from the user's profile box
The following code sample assumes that the class that displayed the
MyUserProfileBoxScreen has passed a reference to the
application's associated
BBMPlatformContext object into the
MyUserProfileBoxScreen constructor.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.device.api.system.*;
net.rim.device.api.ui.component.*;
import java.util.*;
public class MyUserProfileBoxScreen extends MainScreen
{
public MyUserProfileBoxScreen(BBMPlatformContext platformContext)
{
UserProfile userProfile = platformContext.getUserProfile();
UserProfileBox profileBox = userProfile.getProfileBox();
if (profileBox.isAccessible())
{
int[] itemIds = profileBox.getItemIds();
if(itemIds.length >= 1) {
profileBox.removeItem(itemIds[0]);
}
}
}
}
Was this information helpful? Send us your comments.