views:

129

answers:

2

Hi there,

is there any way to know whether the net is connected or not in a blackbery device .I have the following code but it was waiting till the network timed out.

int rc = connection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
 throw new IOException("HTTP response code: " + rc);
}

Is there any other way.

+3  A: 

There are several API for getting network info:

RadioInfo.isDataServiceOperational();
CoverageInfo.isOutOfCoverage();
WLANInfo.getWLANState();
Max Gontar
+1 RadioInfo.getState() might also be helpful in an effort to create a robust/useful error messages for the users.
Fostah
+2  A: 

The title of this question has a different meaning than the body of the question. Based on the title, you can be notified when the network starts by using the RadioStatusListener interface, which defines a networkStarted() function. You could then use the checks that coldice recommends to make sure that the current network supports data transfer.

RadioStatusListner JavaDocs

Fostah
I def second this, specifically the networkStateChange(int state) method within RadioStatusListner. This will save you the trouble of having a background thread that always checks the network state. Though I also recommend checking RadioInfo.isDataServiceOperational() prior to making a data connection each time. Considering that you can get stuck waiting for a timeout, this is a suggested practice.
AtariPete