Robbie Birkett
Robbie Birkett

Reputation: 41

getSystemService(Context.CONNECTIVITY_SERVICE);

Does this method check for an actual connection to the internet or if the device is connected to a wireless network.

I am keen to know that if the device is connected to a router for example but the router is not connected to net or is down would it return true.

My code:-

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager 
                  = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();

Upvotes: 2

Views: 2464

Answers (2)

user3059993
user3059993

Reputation: 344

Your code checks whether device has any internet connection or not. If your device is connected to WI-FI or to any other network then Boolean value true is returned else false is returned.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93542

Well that function doesn't check either, it returns a class that has functionality to check it. But the answer is that it would see the router and return that it has an internet connection. If you want more, have it try to access a webservice on your server (or just download something like the Google homepage).

Upvotes: 0

Related Questions