Skip to content

Instantly share code, notes, and snippets.

@ytabuchi
Created August 24, 2016 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytabuchi/4b5d74c61fe0b5521b4a2d024789e780 to your computer and use it in GitHub Desktop.
Save ytabuchi/4b5d74c61fe0b5521b4a2d024789e780 to your computer and use it in GitHub Desktop.
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