Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Concurrency and Error Handling What To Do When the Network is Down

Can sombody explain me this line of code??

Hello.

I dont get this method can somebody explain it to me please im really confused help please!

    private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;
        }

        return isAvailable;
    }

2 Answers

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Yadin! In Android the NetworkInfo object gives us information about the network the device is currently connected to. So we start off by using the ConnectivityManager to get the current NetworkInfo. Then we check to make sure that the NetworkInfo actually exists (isn't null), and that it 'isConnected()', and if it is we set 'isAvailable' to true. At the end we just return whether or not the network was available. Hope that helps!

Hello Ben thanks for the answer! just 1 more question...what the purpose of this ConnectivityManager please?

Ben Deitch
Ben Deitch
Treehouse Teacher

The ConnectivityManager just contains a ton of information about the state of the device's network connection. It's just what we have to use to get access to the NetworkInfo object.

Oh ok thank you very much! :)