Felipe
Felipe

Reputation: 58

How to have my android app detect whether there is an internet connection or not in ARC?

1) My app monitors internet connection using a BroadcastReceiver but when running on ARC, disabling WIFI doesn't trigger any events.

    <receiver
        android:name=".NetworkReceiver"
        android:label="Network Monitor" >
        <intent-filter android:priority="1" >
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

2) I tried to check the network status using:

final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
boolean isConnected = ni!=null && ni.isConnected();

but it shows as connected even when the "host" is not connected to any network.

3) I tried to keep requesting a URL and checking whether an exception occurs but this only works on ARC running on my desktop. When I published the app to the chromebook the request just handles (never times out) even setting the ConnectTimeout. Interesting enough if there is no connection when I launch the app it will raise an exception. The problem is only if disable the WIFI with my app running - then a request will just "hang" (only in the chromebook, it works on ARC/Mac).

URL url = new URL("http://www.google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(1000);
connection.connect();

Any suggestions?

Upvotes: 1

Views: 315

Answers (1)

Lloyd Pique
Lloyd Pique

Reputation: 916

As you discovered, we do not properly handle the case for the network connection being down. I wouldn't be surprised if there were other edge cases we aren't handling yet.

Please feel free to file a bug to help us prioritize fixing this and other networking issues.

Getting the connectivity changed event working seems like something we should definitely fix to me.

Upvotes: 1

Related Questions