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
}
}