Check network in Android
How do I check any kind of network 开发者_高级运维status in Android?
if (Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
ConnectivityManager.TYPE_MOBILE)
|| Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
ConnectivityManager.TYPE_WIFI) {
// do something
}
For checking the network this function will help you.
`public boolean InternetConnection()
{
ConnectivityManager connectivity = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
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;
}`
if the network is there it will return true otherwise it will return false.
can check network availability or not
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
精彩评论