Monday, March 7, 2011

Android Network How to make sure the network is available?

By Magesh Kumar   Posted at  4:54 AM   Android No comments


This function will return true if the network is available, false if it is not (airplane mode, out of reach, etc.). IMPORTANT : Don't forget to add [HTML_REMOVED] in your manifest, or the line NetworkInfo[] info = connectivity.getAllNetworkInfo(); will crash ! 
public boolean isNetworkAvailable() {
 Context context = getApplicationContext();
 ConnectivityManager connectivity = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
 if (connectivity == null) {
  boitealerte(this.getString(R.string.alert),"getSystemService rend null");
 } else {
  NetworkInfo[] info = connectivity.getAllNetworkInfo();
  if (info != null) {
   for (int i = 0; i < info.length; i++) {
    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
     return true;
    }
   }
  }
 }
 return false;
}

About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

0 comments:

Back to top ↑
Connect with Us

What they says

© 2013 MaGeSH 2 help. WP Mythemeshop Converted by BloggerTheme9
Blogger templates. Proudly Powered by Blogger.