标签:
android 如何检查网络连接状态,是android开发中一个常见的问题。
获取网络信息需要在AndroidManifest.xml文件中加入相应的权限:
<span style="font-size:24px;"><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> </span>1)判断是否有网络连接
<span style="font-size:24px;">public boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } </span>
2)判断WIFI网络是否可用
<span style="font-size:24px;">public boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); //WIFI if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; } </span>3)判断MOBILE网络是否可用
<span style="font-size:24px;">public boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; } </span>4)获取当前网络连接的类型信息
<span style="font-size:24px;">public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; } </span>开发android应用时,涉及到要进行网络访问,时常需要进行网络状态的检查,以提供给用户必要的提醒。一般可以通过ConnectivityManager来完成该工作。
当我们要在程序中监听网络状态时,只要一下几个步骤即可:
1、定义一个Receiver重载其中的onReceive函数,在其中完成所需要的功能,如根据WIFI和GPRS是否断开来改变空间的外观
<span style="font-size:24px;">onnectionReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) { Log.i(TAG, "unconnect"); // unconnect network }else { // connect network } } }; </span>2、在适当的地方注册Receiver,可以在程序中注册,在onCreate中调用如下函数即可:
<span style="font-size:24px;">IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(connectionReceiver, intentFilter); </span>
<span style="font-size:24px;">if (connectionReceiver != null) { unregisterReceiver(connectionReceiver); } </span>在测试中遇到过这样的状况,将一个当前连接wifi的路由设备关闭,但是程序并没有捕捉到unconnect network,可能是因为手机设备立刻连接另一个路由设备了
Android 监控网络状态
<span style="font-size:24px;">public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { Log.i("NetWorkState", "Unavailabel"); return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { Log.i("NetWorkState", "Availabel"); return true; } } } } return false; } </span>
上面这个方法就是判断网络是否连接的代码,返回true表示有网络,返回false表示无网络。 在Android网络应用程序开发中,经常要判断网络连接是否可用,因此经常有必要监听网络状态的变化。android的网络状态监听可以用BroadcastReceiver来接收网络状态改变的广 播,具体实现如下
<span style="font-size:24px;">@Override public void onReceive(Context context, Intent intent) { Log.e(TAG, "网络状态改变"); boolean success = false; //获得网络连接服务 ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); // State state = connManager.getActiveNetworkInfo().getState(); State state = connManager.getNetworkInfo( ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态 if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络 success = true; } state = connManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态 if (State.CONNECTED != state) { // 判断是否正在使用GPRS网络 success = true; } if (!success) { Toast.makeText(LocationMapActivity.this, "您的网络连接已中断", Toast.LENGTH_LONG).show(); } } </span>
<span style="font-size:24px;">/注册网络监听 IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mNetworkStateReceiver, filter); //在Activity中的onDestroy中:' unregisterReceiver(mNetworkStateReceiver); //取消监听 </span>很多朋友在android开发中,都会遇到手机网络类型判断,因为就目前的android平台手机来说:可能会存在4中状态
<span style="font-size:24px;">public static int getAPNType(Context context){ int netType = -1; ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if(networkInfo==null){ return netType; } int nType = networkInfo.getType(); if(nType==ConnectivityManager.TYPE_MOBILE){ Log.e("networkInfo.getExtraInfo()", "networkInfo.getExtraInfo() is "+networkInfo.getExtraInfo()); if(networkInfo.getExtraInfo().toLowerCase().equals("cmnet")){ netType = CMNET; } else{ netType = CMWAP; } } else if(nType==ConnectivityManager.TYPE_WIFI){ netType = WIFI; } return netType; } </span>
标签:
原文地址:http://blog.csdn.net/tuke_tuke/article/details/51684042