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 trialyadin michaeli
Courses Plus Student 5,423 PointsCan 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
Treehouse TeacherHey 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!
yadin michaeli
Courses Plus Student 5,423 PointsOh ok thank you very much! :)
yadin michaeli
Courses Plus Student 5,423 Pointsyadin michaeli
Courses Plus Student 5,423 PointsHello Ben thanks for the answer! just 1 more question...what the purpose of this ConnectivityManager please?
Ben Deitch
Treehouse TeacherBen Deitch
Treehouse TeacherThe 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.