The following code will help you post to Facebook or Twitter from a BlackBerry Java application. This code requires the use of some API elements that were not introduced until OS 7 so you will want to use a different build in order to continue to support older devices. This code was used in version 3.1 of the game Pixelated.
Code for posting to Facebook
public void postToFacebook(String text)
{
JSONObject context = new JSONObject();
MenuItem fb = null;
try
{
context.put(SendCommandContextKeys.TEXT, text);
SendCommand[] sendCommands = SendCommandRepository.getInstance().get(SendCommand.TYPE_TEXT, context, false);if (sendCommands != null && sendCommands.length > 0)
{
SendCommandMenu scm = new SendCommandMenu(sendCommands, 0, 0);
SendCommandMenuItem[] scmi = scm.getSendCommandMenuItems();
for(int i=0;i<scmi.length;i++)
{
if(scmi[i].toString().equals("Facebook"))
{
fb = scmi[i];
}
}
fb.run();
}
}
catch(Exception e){}
}
Code for posting to Twitter
public void postToTwitter(String text)
{
JSONObject context = new JSONObject();
MenuItem tw = null;
try
{
context.put(SendCommandContextKeys.TEXT, text);
SendCommand[] sendCommands = SendCommandRepository.getInstance().get(SendCommand.TYPE_TEXT, context, false);if (sendCommands != null && sendCommands.length > 0)
{
SendCommandMenu scm = new SendCommandMenu(sendCommands, 0, 0);
SendCommandMenuItem[] scmi = scm.getSendCommandMenuItems();
for(int i=0;i<scmi.length;i++)
{
if(scmi[i].toString().equals("Twitter"))
{
tw = scmi[i];
}
}
tw.run();
}
}
catch(Exception e){}
}