Android中判断网络是否可用的代码分享
packagecn.hackcoder.beautyreader.broadcast; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.net.ConnectivityManager; importandroid.util.Log; importandroid.widget.Toast; importcn.hackcoder.beautyreader.activity.base.BaseActivity; importcn.hackcoder.beautyreader.utils.NetWorkUtils; publicclassNetWorkStatusReceiverextendsBroadcastReceiver{ publicNetWorkStatusReceiver(){ } @Override publicvoidonReceive(Contextcontext,Intentintent){ Stringaction=intent.getAction(); if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){ Toast.makeText(context,"networkchanged",Toast.LENGTH_LONG).show(); BaseActivity.isNetWorkConnected=NetWorkUtils.getAPNType(context)>0; } } }
packagecn.hackcoder.beautyreader.utils; importandroid.content.Context; importandroid.net.ConnectivityManager; importandroid.net.NetworkInfo; importandroid.telephony.TelephonyManager; /** *Createdbyhackcoderon15-1-25. */ publicclassNetWorkUtils{ /** *判断是否有网络连接 *@paramcontext *@return */ publicstaticbooleanisNetworkConnected(Contextcontext){ if(context!=null){ ConnectivityManagermConnectivityManager=(ConnectivityManager)context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfomNetworkInfo=mConnectivityManager.getActiveNetworkInfo(); if(mNetworkInfo!=null){ returnmNetworkInfo.isAvailable(); } } returnfalse; } /** *判断WIFI网络是否可用 *@paramcontext *@return */ publicstaticbooleanisWifiConnected(Contextcontext){ if(context!=null){ ConnectivityManagermConnectivityManager=(ConnectivityManager)context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfomWiFiNetworkInfo=mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if(mWiFiNetworkInfo!=null){ returnmWiFiNetworkInfo.isAvailable(); } } returnfalse; } /** *判断MOBILE网络是否可用 *@paramcontext *@return */ publicstaticbooleanisMobileConnected(Contextcontext){ if(context!=null){ ConnectivityManagermConnectivityManager=(ConnectivityManager)context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfomMobileNetworkInfo=mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if(mMobileNetworkInfo!=null){ returnmMobileNetworkInfo.isAvailable(); } } returnfalse; } /** *获取当前网络连接的类型信息 *@paramcontext *@return */ publicstaticintgetConnectedType(Contextcontext){ if(context!=null){ ConnectivityManagermConnectivityManager=(ConnectivityManager)context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfomNetworkInfo=mConnectivityManager.getActiveNetworkInfo(); if(mNetworkInfo!=null&&mNetworkInfo.isAvailable()){ returnmNetworkInfo.getType(); } } return-1; } /** *获取当前的网络状态:没有网络0:WIFI网络1:3G网络2:2G网络3 * *@paramcontext *@return */ publicstaticintgetAPNType(Contextcontext){ intnetType=0; ConnectivityManagerconnMgr=(ConnectivityManager)context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfonetworkInfo=connMgr.getActiveNetworkInfo(); if(networkInfo==null){ returnnetType; } intnType=networkInfo.getType(); if(nType==ConnectivityManager.TYPE_WIFI){ netType=1;//wifi }elseif(nType==ConnectivityManager.TYPE_MOBILE){ intnSubType=networkInfo.getSubtype(); TelephonyManagermTelephony=(TelephonyManager)context .getSystemService(Context.TELEPHONY_SERVICE); if(nSubType==TelephonyManager.NETWORK_TYPE_UMTS &&!mTelephony.isNetworkRoaming()){ netType=2;//3G }else{ netType=3;//2G } } returnnetType; } }
注册:
<receiver android:name=".broadcast.NetWorkStatusReceiver" android:enabled="true" android:exported="true"> <intent-filter> <actionandroid:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver>