BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 09-22-2005, 05:42 AM   #1 (permalink)
New Member
 
Join Date: Aug 2005
Model: 7250
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Mail API: Folder Listener doesnt work on handheld

Please Login to Remove!

Hi All,

I am implementing a simple program that needs to check the inbox for emails with a given subject.

To achieve this, I get a handle of the inbox and attach a listener to it. Whenever a new mail arrives at the inbox, the messagesAdded(FolderEvent e) event is called.

The code works fine in simulated environment and has been tested on handheld simulators for versions 3.6 to 4.0. But it fails on a real handheld. Even if a new mail arrives the listener is not notified. No exception is thrown either.

I am attaching code snippets below. Any help will be really appreciated.

-Code Snippet BEGIN-
...
Store store = Session.getDefaultInstance().getStore();
Folder[] folders = store.list(Folder.INBOX);
Folder inbox = folders[0];
inbox.addFolderListener(this);
...
public synchronized void messagesAdded(FolderEvent e){
Message msg = e.getMessage();
//take some action
}
-Code Snippet END-

Thanks
Vibhor
Offline  
Old 09-22-2005, 11:03 AM   #2 (permalink)
Knows Where the Search Button Is
 
Join Date: Mar 2005
Model: 9000
Carrier: Tmobile
Posts: 31
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is your app signed using the RIM keys as without them you cannot use the mail api on the device. It is a secure API.
Offline  
Old 09-23-2005, 12:59 AM   #3 (permalink)
New Member
 
Join Date: Aug 2005
Model: 7250
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Phil,

The app is signed.

I tried another approach wherein I am polling the inbox every 30 seconds and checking the mails with a particular subject. The polling is happening fine (logs whenever the thread wakes up) but it does not seem to scan the inbox. No exceptions are thrown either.

It is being tested on 7750 Verizon
Platform 1.5.0.30
App/OS version: 4.0.0.171

You could try out the following piece of code -
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.Folder;
import net.rim.blackberry.api.mail.Store;
import net.rim.blackberry.api.mail.event.FolderEvent;
import net.rim.blackberry.api.mail.event.FolderListener;
import net.rim.blackberry.api.mail.Message;
import net.rim.device.api.system.EventLogger;

public class Example extends Thread
{
public static final String SUBJECT = "<SUBJECT>";

public static final int SLEEP_TIME = 10 * 1000;

/**
* Id required for Event logging.
* Key is: Example
*/
private long guid = 0x52ae3a156ed010fL;

public Example()
{
EventLogger.register(guid, "Example", EventLogger.VIEWER_STRING);
EventLogger.setMinimumLevel(EventLogger.INFORMATI ON);

EventLogger.logEvent(guid, "Example Poll Agent Started".getBytes(), EventLogger.INFORMATION);
}

public void run()
{
try{
Store store = Session.getDefaultInstance().getStore();
Folder[] folders = store.list(Folder.INBOX);
Folder inbox = folders[0];
String subject = null;
while (true)
{
//Remove the message from the sent items if stored:
Message [] msgs = inbox.getMessages();
StringBuffer sb = new StringBuffer("Deleted messages: ");
for(int i=0; i < msgs.length; i++)
{
subject = msgs<i>.getSubject();
EventLogger.logEvent(guid, ("Mail Subject: "+subject).getBytes(), EventLogger.INFORMATION);
if ((subject != null) && (!subject.equals("")) && (subject.startsWith(SUBJECT)))
{
sb.append(subject).append(";");
inbox.deleteMessage(msgs<i>);
}
}
EventLogger.logEvent(guid, sb.toString().getBytes(), EventLogger.INFORMATION);
sb = null;
try{
Thread.sleep(SLEEP_TIME);
}
catch (Exception e){
EventLogger.logEvent(guid, e.toString().getBytes(), EventLogger.ERROR);
}
}
}
catch (Exception e){
EventLogger.logEvent(guid, e.toString().getBytes(), EventLogger.ERROR);
}

EventLogger.logEvent(guid, "Example Poll Agent Stopped.".getBytes(), EventLogger.INFORMATION);
}

public static void main(String[] args)
{
Example Example = new Example();
Example.start();
}
}
Offline  
Old 09-26-2005, 08:28 AM   #4 (permalink)
New Member
 
Join Date: Mar 2005
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have a listener in my code that also listens for messages arriving in the Inbox with a particular subject, here's how:

publicclass MailListener implements FolderListener
{
public MailListener()
{
Session.getDefaultInstance().getStore().addFolderL istener(this);
}

public void messagesAdded(FolderEvent e)
{
Message m = e.getMessage();
if(m.isInbound() && m.getSubject().equals(MY_SUBJECT"))
{
//DO STUFF
}
}
}

Hope this helps!
Mandy
Offline  
Old 09-29-2005, 08:01 AM   #5 (permalink)
New Member
 
Join Date: Aug 2005
Model: 7250
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a bunch Mandy. That did the trick.

It seems that listening on or polling the Inbox didnt work at all. Possible reasons could be that the actual inbox where the mails were getting added wasnt at the root level. Putting a listener on the "Store" itself was the key.

Thanks once again,
Vibhor
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.