/
NetworkInformation.cs
49 lines (42 loc) · 1006 Bytes
/
NetworkInformation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using Android.Content;
using Android.Net;
using TecDemo.Mobile.Contracts.Networking;
namespace TecDemo.Mobile.Android.Networking
{
public class NetworkInformation : BroadcastReceiver, INetworkInformation
{
private bool _isConnected;
public bool IsConnected
{
get { return _isConnected; }
private set
{
if (value != _isConnected)
{
_isConnected = value;
DoConnectionStateChanged(value);
}
}
}
private void DoConnectionStateChanged(bool value)
{
var handler = OnConnectionStateChanged;
if (null != handler)
{
handler(value);
}
}
public event ConnectionStateHandler OnConnectionStateChanged;
public override void OnReceive(Context context, Intent intent)
{
var connectivityManager = (ConnectivityManager) context.GetSystemService(Context.ConnectivityService);
var info = connectivityManager.ActiveNetworkInfo;
if (info == null)
{
IsConnected = false;
return;
}
IsConnected = info.IsConnected;
}
}
}