Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
frandelbetis
Posts: 42
Registered: ‎03-17-2012
My Device: Blackberry playbook
My Carrier: none

AddFolderListener works in some devices but in another doesn´t work.

Hello, I´m developing an app where it listens the messages from an email account and it does an specific action. For that, I use this: 

public class EmailListener implements FolderListener {

 

public void registerEmailListener(boolean register) {
ServiceBook sb = ServiceBook.getSB();
ServiceRecord[] srs = sb.findRecordsByCid("CMIME");

if (srs != null) {

ServiceRecord sr;
ServiceConfiguration sc;

/* unregister all email listener  to avoid duplicate listeners */
desregisterAllEmailListeners(srs);

for (int i = srs.length - 1; i >= 0; --i) {
sr = srs[i];
try {
sc = new ServiceConfiguration(sr);

registerEmail(sc, register);

} catch (Exception e) {
e.printStackTrace();
}
}

}

}

 

private void registerEmail(ServiceConfiguration sc, boolean register) {
Session s = Session.getInstance(sc);
if (s != null) {
Folder[] folders = s.getStore().list();
for (int foldercnt = folders.length - 1; foldercnt >= 0; --foldercnt) {
Folder f = folders[foldercnt];
// If the folder doesn't have the service book for this email, we do not want to register it
if (f.getFullName().indexOf(sc.getEmailAddress()) >= 0) {
recurse(f, register);
}
}

}

private void recurse(Folder f, boolean add) {
if (f.getType() == Folder.INBOX) {

f.removeFolderListener(this);
if (add) {
f.addFolderListener(this);
}
}
Folder[] farray = f.list();
for (int fcnt = farray.length - 1; fcnt >= 0; --fcnt) {
recurse(farray[fcnt], add);
}
}

 

public void desregisterAllEmailListeners(ServiceRecord[] srs) {

ServiceRecord sr;
ServiceConfiguration sc;

for (int i = srs.length - 1; i >= 0; --i) {
sr = srs[i];
try {
sc = new ServiceConfiguration(sr);

registerEmail(sc, false);

} catch (Exception e) {
e.printStackTrace();
}
}

}

 

public void messagesAdded(final FolderEvent e) {

 

....

 

}

 

....

 

}

 

When an email has come, it executes messageAdded and does the specific action.  The problem is that in some devices it works, and in another doesn´t work. It registers the email listener correctly in every case, but in some cases don´t execute messageAdded. 

What´s the problem?

 

It works in: 9300 OS 6.0 and OS 5.0 and 9220 OS 7.1

It doesn´t work in 9320 OS 7.1, 9380 OS 7.1 and 9800 OS 6.0

 

Thank you very much.

 

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: AddFolderListener works in some devices but in another doesn´t work.

"Doesn't work" is not a very useful problem description.  Can you describe exactly what happens?  I presume you are debugging on the device, can you tell us how far through your code it gets, what exceptions are thrown, etc...

 

I think you need some additional logging in this code so that you can actually catch problems as they occur.  WIthout it you are in a difficult situation with problems like this, especially when they occur in the wild.  You can't always debug on device.

 

In addition, you will not see anything from this:

 

} catch (Exception e) {
e.printStackTrace();
}

 

If you want to see a stack Trace, then you need to catch Throwable, as follows:

 

} catch (Throwable t) {
t.printStackTrace();

System.out.printlin("Exception : " + t.toString());
}

Please use plain text.
Contributor
frandelbetis
Posts: 42
Registered: ‎03-17-2012
My Device: Blackberry playbook
My Carrier: none

Re: AddFolderListener works in some devices but in another doesn´t work.

Thank you for reply.

In my real code I don´t use t.printStackTrace();  it is a summary of my code. I use a log, but I haven´t shown here. My problem is that, when I do f.addFolderListener(this); it works (doesn´t show me any exception and any of my logs) but when I receive a message to my email account (with this listener) doesn´t show any logs of messageAdded.

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: AddFolderListener works in some devices but in another doesn´t work.

OK, sorry my comment was probably not justified.  Ignore that.

 

I see you have based your code on this KB article:

http://supportforums.blackberry.com/t5/Java-Development/Application-is-not-notified-when-new-message...

 

My quick review suggests the code is fine too.

 

So I must admit I am at a loss to explain your problem. 

 

Do you do something 100% safe right at the start of your listener, say

System.out.println(...)

to make 100% sure that the listener is never invoked?

 

Given I think the registration code is working, the suspicion then fails on the listener which I don't think you have shown us.

 

Please use plain text.
Contributor
frandelbetis
Posts: 42
Registered: ‎03-17-2012
My Device: Blackberry playbook
My Carrier: none

Re: AddFolderListener works in some devices but in another doesn´t work.

[ Edited ]

Yes, I have a class Logger  where I set EventLoggers. In the begin of messageAdded method I show a Logger.

When I receive a message to my email, this log doesn´t appear. (This Logger class works perfecly in others parts of the code)

 

 

public void messagesAdded(final FolderEvent e) {
new Thread(new Runnable() {
public void run() {
try {
logger.logDebug("EmailListener.messagesAdded BEGIN");
Message mail = e.getMessage();

......

}

 

What´s the optimal setting of emails accounts for this to work, I mean, only works the default account? it works without bis?, etc

Please use plain text.
Contributor
frandelbetis
Posts: 42
Registered: ‎03-17-2012
My Device: Blackberry playbook
My Carrier: none

Re: AddFolderListener works in some devices but in another doesn´t work.

Yes!!, it executes messageAdded....debugging. I don´t know why doesn´t show the logs in some devices....

 

The moral is:

Don´t trust just in logEvent, use this and Debug tool, both!! 

Please use plain text.