Creating a context object

When you use the SendCommandRepository class to retrieve a set of commands to include in a Send menu, you must provide a context object. A context object contains information that is required to send content from your application to a recipient. For example, if your application allows a BlackBerry device user to send text and the user chooses to send the text in an email message, the context object might contain the subject and body of the email message to send.

The Send Menu API uses a JSONObject object to represent context information. A JSONObject is an unordered collection of key-value pairs. The keys are String objects, and the values can include several types, such as boolean, int, Hashtable, or Vector.

You can invoke put() of the JSONObject class to add context information to a JSONObject. The Send Menu API provides a set of predefined keys, which are constants in the SendCommandContextKeys class, that you can use to add context information to a JSONObject.

The following code sample demonstrates how to create a JSONObject and add text to it using the SendCommandContextKeys.TEXT key. The put() method can throw a JSONException to indicate that the key that you specify is null, so it's important to catch this exception.

JSONObject context = new JSONObject();
try
{
    context.put(SendCommandContextKeys.TEXT, "This is some text to send.");
}
catch (JSONException e)
{
    System.out.println(e.toString());
}

Was this information helpful? Send us your comments.