NetworkInfo.Type已过时:“已弃用”

时间:2018-08-23 13:37:31

标签: c# android xamarin xamarin.android

例如,我有此方法:

public static bool IsConnectedMobile
        {
            get
            {
                var info = ConnectivityManagers.ActiveNetworkInfo;
                return info != null && info.IsConnected && info.Type == ConnectivityType.Mobile;
            }
        }

我在这里得到一个错误:info.Type == ConnectivityType.Mobile;

enter image description here

我的问题是,我可以找到替代方法吗? (不使用其他库)

2 个答案:

答案 0 :(得分:1)

ActiveNetworkInfo,您需要从GetSystemService中获取它只需使用此代码

ConnectivityManager cm = (ConnectivityManager)this.GetSystemService(Context.ConnectivityService);
NetworkInfo activeNetwork = cm.ActiveNetworkInfo;
if (activeNetwork != null)
{ 
   //connected to the internet
}
 else
{
  //not connected to the internet
}

&有关更多信息,请通过this线程

答案 1 :(得分:0)

此功能是特定于平台的。基本上,您可以使用以下代码检查网络连接状态:

var cm = (ConnectivityManager)GetSystemService(Context.ConnectivityService);
bool isConnected = cm.ActiveNetworkInfo.IsConnected;

应该很简单。

谢谢。

相关问题