BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 09-22-2008, 12:05 PM   #1
danietepa
Knows Where the Search Button Is
 
Join Date: Jul 2008
Model: 8310
PIN: N/A
Carrier: ddd
Posts: 18
Default FolderListener.

Please Login to Remove!

Hi, I have a doubt, my application sent each time the true position, but I want to externally control the period to which they send data. For this I send an email to the application to change the parameter. For this I am using FolderListener but apparently not working. What if so, or I'm confused, thank you.


Code:
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 {
	            //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);
				//store.addFolderListener(this);
				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
		
	}

}
Offline  
Old 09-24-2008, 04:44 AM   #2
danietepa
Knows Where the Search Button Is
 
Join Date: Jul 2008
Model: 8310
PIN: N/A
Carrier: ddd
Posts: 18
Default

Anybody?
thx
Offline  
Old 09-24-2008, 06:25 AM   #3
Ivanov
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Default

works fine for me.
... excepting that you are not correctly getting data out of the email...

check this out:
Code:
                String contenido=orginalMessage.getContent().toString();
                System.out.println("Content: " + contenido);
                String body=orginalMessage.getBodyText();
                System.out.println("Body: " + body);

                //TIEMPO_ESPERA=Integer.parseInt(contenido);
                try
                {
                    TIEMPO_ESPERA=Integer.parseInt(body.trim());
                } catch (NumberFormatException nfx)
                {
                    System.out.println("--> wrong format!");
                }
                System.out.println("TIEMPO_ESPERA: " + TIEMPO_ESPERA);
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 09-24-2008, 09:17 AM   #4
danietepa
Knows Where the Search Button Is
 
Join Date: Jul 2008
Model: 8310
PIN: N/A
Carrier: ddd
Posts: 18
Default

A doubt, Ivanov, mail to which you sent the mail is a different to me there or to send the coordinates can be done with it? Thank you.
Offline  
Old 09-24-2008, 09:31 AM   #5
Ivanov
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Default

I've tested it on a simulator with ESS with some modifications without sending the mail. I thought you have problems only with the listener, so it's what I've tested...

sending e-mail part was replaced by
Code:
                        //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);
                        System.out.println(posiciones);
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 09-24-2008, 09:47 AM   #6
danietepa
Knows Where the Search Button Is
 
Join Date: Jul 2008
Model: 8310
PIN: N/A
Carrier: ddd
Posts: 18
Default

The positions of the gps sends to email without problems. My problem is that if I send a message to the blackberry while its taking position appears that the server is occupied. I've tried sending an email to [email address] with the subject "Time".
Offline  
Closed Thread



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


Implement Whisker Switch  picture

Implement Whisker Switch

$50.00



Vertical Float Switch: Sump Accessory -  picture

Vertical Float Switch: Sump Accessory -

$17.99



ELECTRIC COOLING FAN GROUND-ING THERMO-STAT SWITCH RADIATOR TEMP-ERATURE SENSOR picture

ELECTRIC COOLING FAN GROUND-ING THERMO-STAT SWITCH RADIATOR TEMP-ERATURE SENSOR

$17.99



Dusk to Dwan/Day Night Sensor, Photoelectric Switch, Photo Cell Sensor 110-277V picture

Dusk to Dwan/Day Night Sensor, Photoelectric Switch, Photo Cell Sensor 110-277V

$49.28



Square D 9013FSG2J21 Water Pump Pressure Switch, 30/50 PSI, DPST 9013 picture

Square D 9013FSG2J21 Water Pump Pressure Switch, 30/50 PSI, DPST 9013

$21.88



2 Pack 12V 200 Amp Continuous Duty Relay Switch Car Starter Heavy Duty Split ... picture

2 Pack 12V 200 Amp Continuous Duty Relay Switch Car Starter Heavy Duty Split ...

$38.99







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