Forum Xamarin.Android

Even App Close How to run a method whenever Wifi/Mobile Data/Internet Change using BroadCastReceiver

GASRONGASRON Member ✭✭

I want to show internet status in a message even my app is close with the help of toast using broadcast receiver.when user turn on mobile data or wifi then message should be show that wifi enabled or mobile data enabled with the help of toast using a broadcast receiver and when user turn off the mobile data or wifi then message should be show with the help of toast that no internet connection.

i want solution in Xamarin.Android.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    First, get the network status of the phone.

    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
    }
    

    Using broadcast receivers to get updates

    [BroadcastReceiver()]
    public class NetworkStatusBroadcastReceiver: BroadcastReceiver {
    
        public event EventHandler ConnectionStatusChanged;
    
        public override void OnReceive (Context context, Intent intent)
        {
            if (ConnectionStatusChanged != null)
                ConnectionStatusChanged (this, EventArgs.Empty);
        }
    }
    

    Register the broadcast and detect the ConnectionStatusChanged event.

    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);
        }
    }
    

    Tutorial: https://blogs.infosupport.com/xamarin-android-by-example-monitoring-the-network-status/

  • GASRONGASRON Member ✭✭

    jarvan can u make a sample project for me? bcuz i'm newbie in xamarin.android

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited February 12

    I created a basic demo, you can refer to the code.

Sign In or Register to comment.