Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GetNetworkInfo
using Android.Net;
private void DetectNetwork()
{
ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo;
bool isOnline = (activeConnection != null) && activeConnection.IsConnected;
if (isOnline)
{
// Networkのタイプを表示
NetworkInfo.State activeState = activeConnection.GetState();
System.Diagnostics.Debug.WriteLine(activeConnection.TypeName);
// 全接続を取得して、それぞれの接続状態を確認
// GetAllNetworks()は5.0以上でそれ以前はgetAllNetworkInfo()を使用
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
{
Network[] allNetworks = connectivityManager.GetAllNetworks();
foreach (var item in allNetworks)
{
NetworkInfo info = connectivityManager.GetNetworkInfo(item);
var connect = info.IsConnectedOrConnecting ? "cennected" : "disconnected";
System.Diagnostics.Debug.WriteLine($"{info.TypeName} is {connect}");
}
}
else
{
NetworkInfo[] allNetworks = connectivityManager.GetAllNetworkInfo();
foreach (var item in allNetworks)
{
var connect = item.IsConnectedOrConnecting ? "cennected" : "disconnected";
System.Diagnostics.Debug.WriteLine($"{item.TypeName} is {connect}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment