Check if the network is available

5 votes · 5 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 in your manifest, or the line NetworkInfo[] info = connectivity.getAllNetworkInfo(); will crash !

raw ·
copy
· download
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; }
Add a comment

5 Comments

wat is supposed to be in [HTML_REMOVED]??? is that a permission line for android.permission.ACCESS_NETWORK_STATE?

Reply · Sept. 2, 2009, 8:15 a.m.

You're right, a stupid filter removed the XML line.

Reply · Sept. 2, 2009, 9:12 p.m.

It is a very informative and useful post thanks it is good material to read this post increases my knowledgewebsite directories

Reply · July 24, 2011, 4:20 p.m.

Hey, Really great work, I would like to join your blog anyway so please continue sharing with us,hcg

Reply · Oct. 18, 2011, 3:43 p.m.

Wow, nice post,there are many person searching about that now they will find enough resources by your post.Thank you for sharing to us.Please one more post about that.

Reply · Oct. 31, 2011, 4:49 p.m.