Create a context filter

In this task, a context filter is created that replaces the subject in the JSONObject context object with different text.

Import the required classes and interfaces.

import net.rim.blackberry.api.sendmenu.SendCommandContextFilter;

Override makeMenu() of the MainScreen class to create the menu for your application. Add the required code to set up a Send menu, such as invoking super.makeMenu(), creating and populating a context object, and retrieving a list of SendCommand objects.

Before you invoke add() to add the Send menu to the menu of your application, perform the following steps:

Create a new object to represent a context filter. In this task, the MyContextFilter class is defined as a context filter for SendCommand objects.

MyContextFilter filter = new MyContextFilter();

Invoke setSendCommandContextFilter() of the SendCommand class to set the MyContextFilter object as the context filter for a SendCommand. In this task, the emailSendCommand variable represents a SendCommand object to send content using an email message.

emailSendCommand.setSendCommandContextFilter(filter);

Define the MyContextFilter class and implement the SendCommandContextFilter interface.

class MyContextFilter implements SendCommandContextFilter
{

Implement filterContext(). This method is invoked when a BlackBerry device user clicks a Send menu item that has this context filter set.

public JSONObject filterContext(SendCommand sendCommand)
{

Create a new JSONObject object. Invoke getContext() to retrieve the context object of the SendCommand object that the user clicked, and store it in the JSONObject.

JSONObject newContext = sendCommand.getContext();

In a try/catch block, alter the context object as appropriate. In this task, invoke put() of the JSONObject object to replace the subject text in the JSONObject. Catch a JSONException, which indicates that the specified key is null.