Tuesday

Check Internet Connection Available or Not in Android

If you want find or check you have to connected to internet or not. It is simple to code of your application like this:

First you have to create custom class, ConnectionDetector.java

public class ConnectionDetector {
         private Context _context;
             public ConnectionDetector(Context context){
            this._context = context;
        }
        public boolean isOnline(){
            ConnectivityManager cm = (ConnectivityManager)  _context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni != null && ni.isConnected())
            {
                return true;
            }
            return false;
        }
}

Now call to the mainactivity java class,

ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
       
      Boolean  isInternetPresent = cd.isOnline();
        if (isInternetPresent) {
  //Do Something if internet connection
  }

Happy Coding !!

0 comments

Posts a comment

Related Posts  www.prandroid.com...
 
© 2011 Android Programming Tutorails | 2012 Templates
Designed by Blog Thiết Kế
Back to top