03-31-2009 11:17 AM
Hi,
Here's my situation...
I created a background thread based on this article:
Here's my code:
/* Thread that will run in the background */
private final class BackGroundThread extends Thread {
private static final int TIMER = 15; // minutes
private boolean _stop = false;
public synchronized void stop() {
_stop = true;
}
private boolean isCoverageSufficient() {
if ((DeviceInfo.isSimulator()) ||
(RadioInfo.getState() != RadioInfo.STATE_OFF && (RadioInfo.getNetworkService() & RadioInfo.NETWORK_SERVICE_DATA) != 0) && CoverageInfo.isCoverageSufficient(CoverageInfo.COV
return false; // either a Simulator or a real device with sufficient coverage
}
return false; // real device with insufficient coverage
}
public void run() {
CoverageInfo.addListener(new CoverageStatusListener() {
public void coverageStatusChanged(int newCoverage) {
switch(newCoverage) {
case(CoverageInfo.COVERAGE_CARRIER):
System.out.println("COVERAGE_CARRIER");
GPS.this.sendPoints();
break;
case(CoverageInfo.COVERAGE_MDS):
System.out.println("COVERAGE_MDS");
break;
case(CoverageInfo.COVERAGE_NONE):
System.out.println("COVERAGE_NONE");
break;
}
}
});
while (!_stop) {
GPS.this.getPoint(); // try to get a GPS point
// make sure the radio is on and there is data coverage
if (isCoverageSufficient()) {
GPS.this.sendPoints(); // send the _points array to the web server
} else { // blackberry is out of coverage
GPS.this.storePoints(); // store the _points array on the blackberry
}
try {
Thread.sleep(TIMER*1000*60); // pause the thread for a defined number of minutes
} catch (InterruptedException ie) {
System.err.println("Error: " + ie.toString());
}
}
}
}
The "getPoint()" method get a GPS coordinate using LocationProvider.getLocation().
The "sendPoints()" method send the GPS coordinate to a web server using HTTP connection:
...
HttpConnection con = (HttpConnection)Connector.open(URL); // open URL connection...
The "storePoints()" method just store the GPS coordinates on the Blackberry Persistent Store when the BB is out of coverage.
When the Blackberry try to send a GPS coordinate, but cannot because it's out of coverage, the GPS data is stored on the Blackberry. My thread then sleep for 15 minutes before getting another GPS coordinate. If the BB is in coverage at the next 15 minutes interval, both coordinates are sent (the current one and the one (or ones) that were stored on the device).
This is working fine.
However, now, I'd like to implement a CoverageStatusListener so my GPS coordinate would be sent as soon as the BB is in coverage (and not wait for the next 15 minutes).
I tested for "COVERAGE_MDS" but it never seems to get triggered... but "COVERAGE_CARRIER" gets.
I have a post about this here : http://supportforums.blackberry.com/rim/board/mess
My question here is about thread. The "sendPoints()" method is indeed called here:
case(CoverageInfo.COVERAGE_CARRIER):
System.out.println("COVERAGE_CARRIER");
GPS.this.sendPoints();
break;
However, since my thread is sleeping, this method is not able to initiate an HTTP connection using MDS. I receive a timeout error after 2 minutes.
I'm not an expert about threads. I think I have all the necessary pieces of code... but I'm just not sure how to re-arrange all this.
Can someone help?
Thanks!
Solved! Go to Solution.
04-10-2009 04:09 PM