Question

I'm attempting to implement a BroadcastReceiver that will tell me when the network status has changed. I'll be using that to sync local data back to the main server when I get internet connection back after losing it.

As a start I was looking at this example: http://fizzylogic.nl/2013/08/17/xamarin-android-by-example-monitoring-the-network-status/

public class NetworkStatusMonitor  
{
    private NetworkState _state;

    public NetworkStatusMonitor ()
    {
    }

    public NetworkState State {
        get { 
            UpdateNetworkStatus ();

            return _state; 
        }
    }

    public void UpdateNetworkStatus() {
        _state = NetworkState.Unknown;

        // Retrieve the connectivity manager service
        var connectivityManager = (ConnectivityManager)
            Application.Context.GetSystemService (
            Context.ConnectivityService);

        // Check if the network is connected or connecting.
        // This means that it will be available, 
        // or become available in a few seconds.
        var activeNetworkInfo = connectivityManager.ActiveNetworkInfo;

        if (activeNetworkInfo.IsConnectedOrConnecting) {
            // Now that we know it's connected, determine if we're on WiFi or something else.
            _state = activeNetworkInfo.Type == ConnectivityType.Wifi ?
                NetworkState.ConnectedWifi : NetworkState.ConnectedData;
        } else {
            _state = NetworkState.Disconnected;
        }
    }
}

public enum NetworkState  
{ 
    Unknown,
    ConnectedWifi,
    ConnectedData,
    Disconnected
}

Then my broadcast receiver looks like this:

[BroadcastReceiver()]
public class NetworkStatusBroadcastReceiver: BroadcastReceiver {

    public event EventHandler ConnectionStatusChanged;

    public override void OnReceive (Context context, Intent intent)
    {
        if (ConnectionStatusChanged != null)
            ConnectionStatusChanged (this, EventArgs.Empty);
    }
}

Now my question is... Where do I initialize this and where do I put the following Start() and Stop() methods?

public event EventHandler NetworkStatusChanged;

public void Start ()  
{
    if (_broadcastReceiver != null) {
        throw new InvalidOperationException (
            "Network status monitoring already active.");
    }

    // Create the broadcast receiver and bind the event handler
    // so that the app gets updates of the network connectivity status
    _broadcastReceiver = new NetworkStatusBroadcastReceiver ();
    _broadcastReceiver.ConnectionStatusChanged += OnNetworkStatusChanged;

    // Register the broadcast receiver
    Application.Context.RegisterReceiver (_broadcastReceiver, 
      new IntentFilter (ConnectivityManager.ConnectivityAction));
}

void OnNetworkStatusChanged (object sender, EventArgs e)  
{
    var currentStatus = _state;

    UpdateNetworkStatus ();

    if (currentStatus != _state && NetworkStatusChanged != null) {
        NetworkStatusChanged (this, EventArgs.Empty);
    }
}


public void Stop() {  
    if (_broadcastReceiver == null) {
        throw new InvalidOperationException (
            "Network status monitoring not active.");
    }

    // Unregister the receiver so we no longer get updates.
    Application.Context.UnregisterReceiver (_broadcastReceiver);

    // Set the variable to nil, so that we know the receiver is no longer used.
    _broadcastReceiver.ConnectionStatusChanged -= OnNetworkStatusChanged;
    _broadcastReceiver = null;
}

Sorry for the probably silly question but still new to Xamarin and Android.

Was it helpful?

Solution

I think following link is helpful. I get the notification when the network status changes. networkstatusbroadcastreceiver See Bradley's answer on the bottom of the page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top