在android中判断是否有网络连接
注意:网络类型是3g 或者 wifi
在3g或者wifi类型的网络下做一些更新操作
---------------------------------------------------------------------------
public boolean isConnecting()
{
ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mTelephony = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if(info == null || !mConnectivity.getBackgroundDataSetting())
{
return false;
}
int netType = info.getType();
int netSubtype = info.getSubtype();
if(netType == ConnectivityManager.TYPE_WIFI)
{
return info.isConnected();
}
else if(netType == ConnectivityManager.TYPE_MOBILE && netSubtype ==TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming())
{
return info.isConnected();
}
else
{
return false;
}
}
注意:别忘了在AndroidManifest.xml 中加上检查网络的权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Android-判断是否有网络连接&&类型
最新推荐文章于 2023-12-22 14:05:22 发布