Saturday, June 12, 2010

BlackBerry Emails: Detect Email Contents in Apps

In your BlackBerry applications, sometimes you may want to 'intrude' on users' emails. For example, you may want to check the users' typings of keywords in the emails to trigger some feature you designed. Here is the way to get the email content in your application.

[Procedure]
Step 1: Create ViewListenerExtended and add it to Session

You need a ViewListenerExtended to listen the actions of 'Open', 'Close', 'Compose', 'Reply' and 'Forward' Emails. Here, ViewListenerExtended is an interface, you need to override the methods. For example, if you want to detect the moment when the user is composing the email, you need to override the method 'void newMessage (MessageEvent e)'. See Step 2.

Additionally, use Session.addViewListener([the listener you created]) to add the ViewListener in to the application.

Step 2: Override the methods in ViewListenerExtended

In the methods, if you want to get to the email contents, you need to find the manager which contains the email TextField. Here are the sub-steps you need to follow:
(1). Get the Current Email Screen
//Screen Type: net.rim.device.apps.internal.blackberryemail.email.EmailEditorScreen
Screen currentScreen = UiApplication.getUiApplication().getActiveScreen()
(2). Get the VerticalFieldManager from the Email Screen
//Manager Type:
net.rim.device.apps.internal.blackberryemail.email.EmailEditorScreen
$BodyVerticalFieldManager
VerticalFieldManager bodyManager = (VerticalFieldManager) currentScreen.getField(i);
(3). Get the Email TextField from the Manager
//TextField Type: net.rim.device.api.ui.component.ActiveAutoTextEditField
emailTextField = (ActiveAutoTextEditField) bodyManager.getField(j);

Step 3: Add FieldChangeListener on Email Text Field to detect the changes in Emails
(1). Create class to implement FieldChangeListener
(2). Override method 'fieldChanged' in your class
(3). Add your class to Email Text Field you got from Step 2.

You should be able to detect the email contents and trigger your desired features now. Enjoy!



0 comments:

Post a Comment