Created
August 24, 2016 02:03
-
-
Save ytabuchi/4b5d74c61fe0b5521b4a2d024789e780 to your computer and use it in GitHub Desktop.
GetNetworkInfo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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