User profile
You can use the
UserProfile class to access the current user's
BlackBerry
Messenger profile and retrieve or change the user's display name, display picture,
status, and personal message.
You can retrieve a reference to the current user's profile by invoking
the
BBMPlatformContext.getUserProfile() method.
The
UserProfile class also provides retrieval methods for
the current user's
UserProfileBox and
UserProfileLocation instances.
Retrieve the user's profile
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 your
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 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)
{
}
}
-
In the constructor, invoke
BBMPlatformContext.getUserProfile() to retrieve a
reference to the
UserProfile object associated with this
BBMPlatformContext. This returned instance is the
current user's profile.
UserProfile userProfile = platformContext.getUserProfile();
-
In the constructor, retrieve the user's display name, display picture,
status, status message, and personal message. Assign these values to the
variables.
String displayName = userProfile.getDisplayName();
Bitmap displayPicture = userProfile.getDisplayPicture();
String personalMsg = userProfile.getPersonalMessage();
int status = userProfile.getStatus();
String statusMsg = userProfile.getStatusMessage();
Code sample: Retrieving the user's profile
The following code sample assumes that the class that displayed the
MyUserProfileScreen has passed a reference to the
application's associated
BBMPlatformContext object into the
MyUserProfileScreen constructor.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.device.api.ui.container.*;
import java.util.*;
public class MyUserProfileScreen extends MainScreen
{
public MyUserProfileScreen(BBMPlatformContext platformContext)
{
UserProfile userProfile = platformContext.getUserProfile();
String displayName = userProfile.getDisplayName();
Bitmap displayPicture = userProfile.getDisplayPicture();
String personalMsg = userProfile.getPersonalMessage();
int status = userProfile.getStatus();
String statusMsg = userProfile.getStatusMessage();
}
}
Update the user's profile
Before you begin: Make sure that you
have
registered your
application with the BlackBerry Messenger platform, and that the class
that displays the screen passes in a reference to the
BBMPlatformContext object that is associated with this
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
BBMPlatformContext object that is associated with this
application.
public class MyUserProfileScreen extends MainScreen
{
public MyUserProfileScreen(BBMPlatformContext platformContext)
{
}
}
-
In the constructor, invoke
BBMPlatformContext.getUserProfile() to retrieve a
reference to the
UserProfile object that is associated with BBMPlatformContext. The instance that is returned is the
current user's profile.
UserProfile userProfile = platformContext.getUserProfile();
-
Invoke
UserProfile.setStatus(), and pass in a status
and message. A confirmation dialog is displayed when you invoke this method. The following code sample updates the user's status to busy.
You can also update the personal message and display picture by invoking setPersonalMessage() and setDisplayPicture(), respectively.
boolean allowed = userProfile.setStatus(Presence.STATUS_BUSY, "I am busy.");
if(! allowed)
{
int result = Dialog.ask(Dialog.D_YES_NO, "Would you like CoolApp to stop
updating your profile status?");
if(result == Dialog.YES)
{
// Optional code to prevent the application from asking to update the
// user's status
}
}
Code sample: Updating the user's profile
The following code sample assumes that the class that displayed the
MyUserProfileScreen has passed a reference to the
application's associated
BBMPlatformContext object into the
MyUserProfileScreen 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 MyUserProfileScreen extends MainScreen
{
public MyUserProfileScreen(BBMPlatformContext platformContext)
{
UserProfile userProfile = platformContext.getUserProfile();
boolean allowed = userProfile.setStatus(Presence.STATUS_BUSY, "I am busy.");
if(! allowed)
{
int result = Dialog.ask(Dialog.D_YES_NO, "Would you like CoolApp to
stop updating your profile status?");
if(result == Dialog.YES)
{
// Optional code to prevent the application from asking to update
// the user's status
}
}
}
}
Was this information helpful? Send us your comments.