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 !
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;
}
5 Comments
wat is supposed to be in [HTML_REMOVED]??? is that a permission line for android.permission.ACCESS_NETWORK_STATE?
You're right, a stupid filter removed the XML line.
It is a very informative and useful post thanks it is good material to read this post increases my knowledgewebsite directories
Hey, Really great work, I would like to join your blog anyway so please continue sharing with us,hcg
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.