Is it on? An internet connection checker snippet
Most of our apps use the internet in some way, so we made an util class, that we use regularly. To save some bandwidth, I only use it after a connection related exception(ioexception etc) was thrown, but of course checking ahead is a good idea sometimes.
It works like this:
If I get a connection related exception anywhere, I pass it up to the UI, where I call the helper method below.
- public class NetChecker {
- public static boolean isOnline() {
- //ConnectivityManager is used to check available network(s)
- ConnectivityManager cm = (ConnectivityManager) YourContext().getSystemService(Context.CONNECTIVITY_SERVICE);
- if (cm.getActiveNetworkInfo() == null){
- //no network is available
- return false;
- } else {
- //at least one type of network is available
- return true;
- }
- }
- public static enum NetStatus {
- NONET, FAULTYURL, NETOK, CONN_MISSING
- }
- public static NetStatus checkNet() {
- // is there an active connection to any network?
- if (isOnline() == false) {
- return NetStatus.CONN_MISSING;
- }
- // is the server reachable?
- try {
- urlc.setConnectTimeout(1000 * 5); // Timeout is in seconds
- urlc.connect();
- if (urlc.getResponseCode() == 200) {
- //http response is OK
- return NetStatus.NETOK;
- }
- return NetStatus.FAULTYURL;
- return NetStatus.NONET;
- }
- return NetStatus.NETOK;
- }
- }
Helper class for the ui:
- public class NetCheckerHelper {
- public static boolean netCheckerHelper(){
- switch(NetChecker.checkNet()){
- case CONN_MISSING:
- //User should enable wifi/3g etc
- return false;
- case NONET:
- //User has enabled network, but it's not working
- return false;
- case FAULTYURL:
- //Url of the server is faulty
- return false;
- case NETOK:
- //Maybe the problem has been solved since the exception was thrown. Retry the last method that threw the exception
- return true;
- }
- return false;
- }
- }
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 37 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 37 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 37 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 38 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 38 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN