获取网络连接的实例

来源:互联网 发布:特别搞笑的网络歌曲 编辑:程序博客网 时间:2024/06/05 03:59

1.在manifest文件中获取访问网络状态的权限

2.获取网络连接的工具类

int type = 0;ConnectivityManager connectivitymanager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);NetworkInfo netinfo = connectivitymanager.getActiveNetworkInfo();if (netinfo == null) {return type;}if (netinfo.getType() == ConnectivityManager.TYPE_MOBILE) {type = 1;}if (netinfo.getType() == ConnectivityManager.TYPE_WIFI) {type = 2;}

具体的请看demo

http://download.csdn.net/detail/mace_android/9118291

0 0