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!
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
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.
Ken AlgerTreehouse Teacher
I'd have a look at the information in the callout box here: https://developer.android.com/reference/android/net/NetworkInfo
And then at the
ConnectivityManager.NetworkCallback docs: https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback.html
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?