Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

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


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.


  • 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 (
            // 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

    Using broadcast receivers to get updates

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


  • GASRONGASRON Member ✭✭

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

  • 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.