Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


NetworkInfo is deprecated. What to use in order to check if network is available?

I've been looking in Android DOCS for an hour and could not figure this out. Can someone provide code for testing connection availability? I do not want to use deprecated classes and methods.

2 Answers

Thank you so much for reply. I loved the Stormy course and now I am practicing with my skills. You are a great teacher!

I have been working for a bit. I implemented NetworkCallBack and called onAvailable and onUnavailabe methods. I also checked out onLost and onLoosing to see how they work.

I tested onAvailable method and it works fine. I can get data from API when I've access to the internet. However, I could not make onUnavailable work.

  • I tried to show some toasts when my wifi was turned off but it did not show anything.
  • I tried logging but my log messages were not in the logcat.

Maybe I am doing something wrong? is unAvailable method the one I should use when network is unavailable?

And last thing, I've noticed that onUnavailable() checks unavailability based on timeout(Do not know anything about it) property. How to make my app see that connection is unavailable in terms of this timeout?