08-07-2008 01:37 PM
I haven't done much error trapping in the sample code I posted.
To solve this quickly, I would say to move this line "sleep(TIMER*1000);" outside of this condition "if (lp != null) {" (at the end).
This way, the thread will be executed again in the next delay whether or not "lp" was assigned a LocationProvider.
I'm sure there's a nicer way to do it... I'll have to to the same thing on my code... I'll keep you posted.
08-07-2008 01:47 PM
I'm almost done (besides a few error trappings)!
I have one more question.
I am now able to get GPS coordinates from the Blackberry, send them to a Web Server using a background thread that runs automaticaly on startup on a real Blackberry (my code is now signed).
However, even though I don't want my users to do anything manually to trigger my app (hence the run on startup), I'd like to give them the option to see if my app is currently working fine (otherwise, they have no idea if it's working or not).
In other words, I'd like to have an icon on the Blackberry, that when clicked, will display the latest transaction (last updated) and offer the user to start the application (if it has been stopped for whatever reason) on the background or stop the background application.
Eric answered me that in a previous post:
"You won't see an icon on a real device... it runs in the background and shouldn't do any UI either. If you need to do UI you need to build a second, separate app and have the two apps (the background one and the UI one) talk to each other (global events work well for this).
It's usually a good idea to have the UI app check to make sure the background app is still running and if it's not, launch it. Makes it easier to recover from errors in case your background app crashes... users reset devices rarely, after all... "
It's pretty much what I want to achieve.
I just would need a starting point... like a sample, some documentation or some advices.
Thank you!
08-07-2008 07:21 PM
Many Thanks camangroup
I tried what you suggested.
if (lp != null) {
Location l = lp.getLocation(110);
Coordinates c = l.getQualifiedCoordinates(); // get GPS coordinates
if (c != null) {Do this} //sleep(120000);// pause the thread for defined milli-seconds }sleep(120000); //moved for re-running the thread
This should work as I see it as well but it hasnt really done the trick. Not sure why its not re-running the thread again.
Regards
Adi
08-08-2008 05:56 AM - last edited on 08-08-2008 07:34 AM
I am using GPSemailing´s sample but dont send the email.
08-10-2008 07:36 PM
Greetings Again
I am having some trouble trying to keep my application running when GPS is out of range. I have implemented sleep at
if (lp != null)
{ code }
sleep(65000);
But this has not worked for me.
////////
public void run() {
while (!_stop) {
Address a;
Store store = Session.getDefaultInstance().getStore();
Folder[] folders = store.list(Folder.SENT);
Folder sentfolder = folders[0];
Message m = new Message(sentfolder);
//SimpleDateFormat sdf;
try {
LocationProvider lp = LocationProvider.getInstance(null);
if (lp != null) {
Location l = lp.getLocation(60);
Coordinates c = l.getQualifiedCoordinates(); // get GPS coordinates
if (c != null) {
String lat = Double.toString(c.getLatitude()); // latitude
String lng = Double.toString(c.getLongitude()); // longitude
String alt = Float.toString(c.getAltitude()); // altitude
String tim = Long.toString(l.getTimestamp()); // timestamp
String spd = Float.toString(l.getSpeed()); // speed
String orgstr = "0449953853,"+ tim + "," + lat + "," + lng + "," + spd + "," + alt;
String googmap = "http://maps.google.com/maps?q=" + lat + "," + lng;
String finalstr = orgstr + "\n" + googmap;
a = new Address("water.meters@usus.com.au", "Water Meters");
Address[] addresses = {a};
m.addRecipients(Message.RecipientType.TO, addresses);
m.setContent(finalstr);
m.setSubject("GPS Location");
Transport.send(m);
}
}sleep(65000); //moved for re-running the thread
}catch (Exception e) {
System.err.println("An error has occurred " + e.toString());
System.exit(0);
}
}
}
///////
Please suggest.
Thanks in advance.
Regards
08-11-2008 04:52 AM
Fellow Posters,
I note that this thread has resolved its original question (sending info) and is now on several different topics. Can I suggest that you raise the questions that you have in separate threads with more specific subjects.
I would suggest that:
1) canamgroup raise a question about developing a User Interface to a Background Application, using the post of 08-07-2008 06:47 PM
2) GPSemailing raise a question about loosing GPS, using the post of 08-11-2008 12:36 AM
3) danietepa may consider raising a post, however the question in the post of 08-08-2008 10:56 AM looks very similar to the post from GPSemailing
Brief answers to your questions are:
1) canamgroup - You can develop the application as a UiApplication, then the background task will display an icon. There are articles and previous questions about coding background applications like this - search the forum and the Knowledgebase.
2) GPSemailing - looking at your code, it seems the only way for it to fail is to have an exception and go through your catch. Has it done this? What was the exception?
08-11-2008 05:50 AM
Thanks Peter you have been very helpful.
Sorry about populating this thread this my own questions. I have now started a new thread.
"Emailing GPS Info from BlackBerry curve8310 using Java"
And NO it hasnt thrown an exception, so should I assume that it doesnt go though the catch.
But another thing I noticed that when GPS is in range and after waiting long periods for it to send an email I tried to click the application again then it starts to work normal.
Regards
02-16-2010 11:02 AM
package com.rim.samples.device.gpstrack;
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 javax.microedition.location.*;
import java.io.*;
public final class postGPS extends UiApplication {
private BackGroundThread _thread;
public static void main(String[] args) {
postGPS gps = new postGPS();
gps.enterEventDispatcher();
}
public postGPS() {
_thread = new BackGroundThread(); // create a new separate thread
_thread.start(); // start the thread
}
/* Thread that will run in the background */
private static class BackGroundThread extends Thread {
private static final int TIMER = 30; // seconds
boolean _stop = false;
public synchronized void stop() {
_stop = true;
}
public void run() {
while (!_stop) {
String url = "http://www.bdsaint.mobi/get_bb.php?";
HttpConnection con = null;
OutputStream out = null;
int responseCode;
try {
LocationProvider lp = LocationProvider.getInstance(null);
if (lp != null) {
Location l = lp.getLocation(60);
Coordinates c = l.getQualifiedCoordinates(); // get GPS coordinates
if (c != null) {
String lat = Double.toString(c.getLatitude()); // latitude
String lng = Double.toString(c.getLongitude()); // longitude
String coord = "lat=" + lat + "&long=" + lng; // post data
con = (HttpConnection)Connector.open(url); // open URL connection
con.setRequestMethod(HttpConnection.POST); // POST method
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
out = con.openOutputStream(); // post data as a stream
out.write(coord.getBytes());
responseCode = con.getResponseCode(); // push data and receive response code
if (responseCode != HttpConnection.HTTP_OK) {
System.out.println("HTTP STATUS CODE: 404"); // error
} else {
System.out.println("HTTP STATUS CODE: 200"); // successfull
}
if (con!=null) con.close(); // close the URL connection
}
sleep(TIMER*1000); // pause the thread for a defined number of minutes
}
} catch (Exception e) {
System.err.println("An error has occurred " + e.toString());n I
System.exit(0);
}
}
}
}
}
Above is the code I use in my application. You will notice that it is almost the same code as previously posted with some minor changes. I signed the application and did set it as a System App so that it auto starts without any UI. I tested it with the 8310 Curve Simulator and it worked perfectly. My question however is this. When I install the app on my real 8310 Curve all seems to be working but it does not connect to the HTTP URL. I have no idea why. I did disable the firewall on the phone but that did not work either. Any help will be muched appreciated.