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
Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified
Accepted Solution

Thread Vs Sleep Vs MDS Http Connection Question

Hi,

 

Here's my situation...

 

I created a background thread based on this article:

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

 

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.COVERAGE_MDS)) {
                    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
                    con.setRequestMethod(HttpConnection.POST); // POST method
                    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    OutputStream out = con.openOutputStream(); // post data as a stream
                    out.write(data.getBytes());
                    int responseCode = con.getResponseCode(); // push data and receive response code
                    if (responseCode == HttpConnection.HTTP_OK) {
                        // success
                    }
                    if (con!=null) con.close(); // close the 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/message?board.id=java_dev&thread.id=6076

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!

 

Highlighted
BlackBerry Development Advisor
Posts: 15,697
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Thread Vs Sleep Vs MDS Http Connection Question

The network connection should not be made from within a listener method.  You should not perform any blocking operation or heavy processing from within a listener method.  Instead, you could create a new thread from within the method that could then create the network connection to send your data.
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