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
BlackBerry Development Advisor
MSohm
Posts: 14,757
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: read messages

You could use a FolderListener to accomplish this.  I recommend having a look at this link.

 

What Is - Application is not notified when new messages arrive
Article Number: DB-00153

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800698/What_Is...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: read messages

To find out why your code is not working, I suggest that you debug the 8310 while it is attached to the PC.  Then you can watch it stepping through the messages and figure out why it doesn't find the message you expect.  You will also see the error output, if there is an Exception generated.

 

However your code goes through all the messages currently in your inbox, which seems pretty inefficient.  You might consider coding a FolderListener, so that you will detect an incoming email as soon as it arrives - then you can check for your key word, set a flag, and delete the email too - so it doesn't get processed multiple times.

 

If you are going to try this option, I suggest you review this KB article:

What Is - Application is not notified when new messages arrive
Article Number: DB-00153

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800698/What_Is...

 

You might also consider using SMS to control your application.  Here are some useful links should you consider using SMS:

 

What is - Different ways to listen for SMS messages
Article Number: DB-00579

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is...

 

How To - Use SMS to notify an application 
Article Number: DB-00533

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/How_To_...

 

 

 

 

 

 

New Developer
danietepa
Posts: 23
Registered: ‎08-06-2008
My Device: Not Specified

Re: read messages

Ok, thx  will read those articles and I already tale.
Certainly some manual on how to carry out the debug in the real device on eclipse?. I have never done.
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified
New Developer
danietepa
Posts: 23
Registered: ‎08-06-2008
My Device: Not Specified

Re: read messages

 

I using this code now but dont work, the application dont stop if i send a email, the session is busy, i supose writing the email whith coordenates

 

boolean
flag=false;
 public boolean leerMensaje() throws NoSuchServiceException{

Store store = Session.waitForDefaultSession().getStore();

store.addFolderListener(new FolderListener() {

//implement other FolderListener methods

//...

 

public void messagesAdded(FolderEvent e) { if( e.getMessage().isInbound() == true )

{

if (e.getMessage().getContentType().equals("Forzar")){

flag=true;

}

}

 

}

 

public void messagesRemoved(FolderEvent e) {

// TODO Auto-generated method stub

 

}

});

 

return flag;

 

}

 
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: read messages

I suspect you are confused about how a listener works.  In theory, you set up a listener once, typically at startup time and leave it running.  Its job is to inform you when the message you are waiting for arrives.  It would appear from the code below that you are trying to use a listener in the same way as you used the search.  I suggest you go back to the search mechanism (where you read all the messages in your INBOX).  It is not particularly efficient but at least you understand how it works.  So I would take that option, and do more debugging yourself - attach the device and step through your code, particularly the code that reads the messages.  You can see what messages are on the device, so compare that with what you see when you search.

 

 

New Developer
danietepa
Posts: 23
Registered: ‎08-06-2008
My Device: Not Specified

Re: read messages

Hi, sorry I leave this topic for a while.
I have implemented the code where the sleep changes depending on the message that you receive with the new time, but  apparently not working as it does not do anything when you receive the message.

package com.rim.samples.docs; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*; import javax.microedition.io.*; import net.rim.device.api.io.*; import javax.microedition.location.*; import net.rim.blackberry.api.invoke.*; import net.rim.blackberry.api.mail.*; import net.rim.blackberry.api.mail.event.FolderEvent; import net.rim.blackberry.api.mail.event.FolderListener; import net.rim.device.api.util.*; import java.util.*; import java.io.*; public final class gpsEmail2 extends Application implements FolderListener{ private BackGroundThread _thread; private int TIEMPO_ESPERA=60000; Store store = Session.getDefaultInstance().getStore(); public static void main(String[] args) { gpsEmail2 gps = new gpsEmail2(); gps.enterEventDispatcher(); } public gpsEmail2() { try { //retrieve the default message service //add the listener store.addFolderListener(this); } catch (Exception e) { System.out.println(e.toString()); } _thread = new BackGroundThread(); // create a new separate thread _thread.start(); // start the thread } /* Thread that will run in the background */ private class BackGroundThread extends Thread{ private static final int TIMER = 30; // seconds private static final String ID_VEHICULO = "1"; boolean _stop = false; public synchronized void stop() { _stop = true; } public void run() { while (!_stop) { String posiciones = ""; String finalstr = ""; Address a; Folder[] folders = store.list(Folder.SENT); Folder sentfolder = folders[0]; Message m = new Message(sentfolder); try { LocationProvider lp = LocationProvider.getInstance(null); if (lp != null) { for (int i = 0; i < 4; i++) { Location l = lp.getLocation(300); Coordinates c = l.getQualifiedCoordinates(); // Obtenemos las coordenadas if (c != null) { // Se recuperan cada una de las variables // necesarias String latitud = Double.toString(c.getLatitude()); // latitud String longitud = Double.toString(c.getLongitude()); // longitud String altitud = Float.toString(c.getAltitude()); // altitud String tiempo = Long.toString(l.getTimestamp()); // Tiempo String velocidad = Float.toString(l.getSpeed()); // Velocidad String orgstr = "B001" + ";" + tiempo + ";" + latitud + ";" + longitud + ";" + velocidad + ";" + altitud; String googmap = "http://maps.google.com/maps?q="+ latitud + "," + longitud; // Se meten en un String finalstr = orgstr + ";" + googmap + "\n"; posiciones = posiciones + finalstr; sleep(TIEMPO_ESPERA); } } a = new Address("daniel@mpginfo.com","daniel@mail.mpginfo.com"); Address[] addresses = { a }; m.addRecipients(Message.RecipientType.TO,addresses); m.setContent(posiciones); m.setSubject("Localización del GPS"); Transport.send(m); //sleep(60000); // pause the thread for a defined number // of minutes } } catch (Exception e) { System.err.println("Un error ha ocurrido " + e.toString()); // System.exit(0); } } } } /* Stop the thread on exit */ protected void onExit() { _thread.stop(); } public void messagesAdded(FolderEvent e) { //get the message and folder it was added to Message orginalMessage = e.getMessage(); Folder messageFolder = orginalMessage.getFolder(); //check that the folder was the inbox if (messageFolder.getType() == Folder.INBOX) { System.out.println("Message has arrived"); //get the message subject String subject = orginalMessage.getSubject(); //check the message for the tag if (subject.indexOf("Tiempo") != -1) { System.out.println("Message has tag, activate notification"); String contenido=orginalMessage.getContent().toString(); TIEMPO_ESPERA=Integer.parseInt(contenido); } } } public void messagesRemoved(FolderEvent e) { // TODO Auto-generated method stub } }

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: read messages

At first glance, this code looks OK.

 

Have you debugged the code on the device and stepped through the processing to see what actually happens?  That is what I would do next and it should find the problem.  If not, tell us exactly what happens when a message arrives.

 

 

 

 

New Developer
danietepa
Posts: 23
Registered: ‎08-06-2008
My Device: Not Specified

Re: read messages

Hi, I have debug, but in the room where I do not have coverage gps and therefore gives no exception to be covered. I have tried it outside the room but apparently did nothing when he sent the email to the device.
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: read messages

You are trying to debug the problem with processing the emails, and checking that it correctly sets your variable TIEMPO_ESPERA. 

 

So we need to find out if the listener detects the email and if it sets this value correctly - this you can do by debugging your current code.  When you are happy that the email detection is working, then you can start looking at the processing in the GPS loop.  I suspect your location provider request will eventually timeout, so you can debug at that point and check the value in the variables.  if it doesn't break out, then remove that code and put something simple in there (like a sleep), so you can check it.

 

Your problem is specific to extracting details from an email.  If you have to change your other code to debug this problem, then that is what you have got to do!

 

Can you remind me why you can't use the Simulator and the Email simulator anyway?