团队博客4

对于手机网络状态的识别:分为有网、无网,有网有:2G、3G、4G和WIFI;

对于手机网络状态的识别使用ConnectivityManager,ConnectivityManager主要管理的是和网络连接相关的操作;

private ConnectivityManager manager;
manager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
boolean connect = activeNetworkInfo.isConnected();
if(connect){
Toast.makeText(getApplicationContext(), "当前的网络连接可用", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "当前的网络连接不可用", Toast.LENGTH_SHORT).show();
}
State state = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
if(State.CONNECTED == state){
Toast.makeText(getApplicationContext(), "GPRS网络已连接", Toast.LENGTH_SHORT).show();
}
state = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if(State.CONNECTED == state){
Toast.makeText(getApplicationContext(), "WIFI网络已连接", Toast.LENGTH_SHORT).show();
}

  在AndroidMainfest.xml中添加权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><!-- 允许应用程序获取网络的状态信息 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><!-- 允许应用程序获取WiFi的状态信息 -->

  

 

posted @ 2016-09-03 22:07  JYJe族  阅读(110)  评论(0编辑  收藏  举报