/**
* 检测网络是否可用
* @return
*/
public boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return ni != null && ni.isConnectedOrConnecting();
}
public class AllBroadcastReceiver extends BroadcastReceiver {
private final String Tag = "AllBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
int networkTtpe= 0;
NetworkInfo netInfo;
String action = intent.getAction();
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
//netInfo = mConnectivityManager.getActiveNetworkInfo();
netInfo = intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
if(NetworkInfo.State.CONNECTED == netInfo.getState()&& netInfo.isAvailable()) {
if(netInfo.getType()==ConnectivityManager.TYPE_WIFI){
/**WiFi网络**/
networkTtpe = 1;
LogUtils.i(Tag, "setNetworkTtpe : " +networkTtpe);
}else if(netInfo.getType()==ConnectivityManager.TYPE_ETHERNET){
/**有线网络**/
networkTtpe = 2;
LogUtils.i(Tag, "setNetworkTtpe : " +networkTtpe);
}else if(netInfo.getType()==ConnectivityManager.TYPE_MOBILE){
/**3g网络 **/
networkTtpe = 3;
LogUtils.i(Tag, "setNetworkTtpe : " +networkTtpe);
}
} else {
/**网络断开 **/
networkTtpe = 0;
LogUtils.i(Tag, "setNetworkTtpe : " +networkTtpe);
}
}
}
}