Posted June 6, 2012 by Rapid John in Developers
 
 

Developers, Be Social – Integrate your apps with Facebook and Twitter

connected-apps-sample
connected-apps-sample

One of the unsung heroes of the BlackBerry® 7 Java SDK is the Send Command Framework. Although the name doesn’t say much, this API lets you seamlessly integrate with existing apps on the device to share data.

Thankfully the native Facebook® and Twitter® apps are two of many!

So no more hair-pulling to figure out how to talk to social networks directly.

There are already apps for them, so let’s learn how to leverage those apps from ours instead of reinventing it.

The first step is to create the data or context we would like to share. This context is simply a JSON object that encapsulates our data.

Here is an example:

JSONObject context = new JSONObject();
try {
context.put(SendCommandContextKeys.TEXT, “Your Text”);
context.put(SendCommandContextKeys.SUBJECT, “Your Text”);
// context.put(SendCommandContextKeys.PATH, “file:///…..”);
} catch (JSONException e) {
System.out.println(e.toString());
}

Intentionally the PATH type data has been commented out because we cannot use PATH data in a context that also has TEXT or SUBJECT types. But I still wanted to highlight that we can also share a file path (e.g. a photo).

Once we have our data context nicely wrapped up in a JSONObject, we need to create SendCommand objects that point to specific apps with a specific context. So how do we get these SendCommands? Easy peasy — we simply query the Send Command Framework with our context data. Here’s how:

SendCommand[] commandsAll =
SendCommandRepository.getInstance().get(SendCommand.TYPE_TEXT, context, true);

Notice that the 3rd parameter above is a Boolean. If true, the query returns all commands regardless if their associated applications can be opened; if false, it returns only commands whose associated applications can be opened. Ideally we would set it to false; however, in this post we will see how we can get them all and filter them in our own application logic.

Each SendCommand object has an ID that uniquely identifies the target app and the context. Note that the IDs are not documented as there could be so many of them, but it is fairly easy to figure them out by experimentation. Since our target is the Facebook app and the Twitter app, let me save you the work by telling you what their IDs are.

Now let’s filter:

for (int i = 0; i < commandsAll.length; i++) {
if (commandsAll[i].getId().equals(“Twitter_text”)) {
commands[0] = commandsAll[i];
}
if (commandsAll[i].getId().equals(“Facebook_text”)) {
commands[1] = commandsAll[i];
}}

…and we are done. We have our SendCommand objects and we are free to call their run() method anywhere in our app. Be it a Button click or a Menu selection, knock yourself out!

You can download the sample app from Github

Did you enjoy this article? If so, we’d love to hear your thoughts on the Forums or on our Facebook page. Get more articles instantly on your BlackBerry smartphone with our Free BlackBerry 10 App.

Via BlackBerry DevBlog

Enjoy this article? Share it with others.

  • Facebook
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Digg
  • Pinterest
  • Google Plus
  • Tumblr
  • Reddit
  • Instapaper
  • Delicious
  • Email
  • Print