Home » Android » Check whether Internet is on or off in Xamarin Android

Check whether Internet is on or off in Xamarin Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am working on Xamarin Android Application.Before proceed to my next fragment I want to check Internet Connection and inform user about it ? How can i implement that ?And how to refresh whole fragment after user switch-on the internet?
Any advice or suggestion will be appreciated !

How to&Answers:

Try this :

NetworkStatus internetStatus = Reachability.InternetConnectionStatus();

if(!Reachability.IsHostReachable("http://google.com")) {
    // Put alternative content/message here
}
else
{
    // Put Internet Required Code here
}

Answer:

To get the network status you could use the following method in your activity:

 public bool IsOnline()
    {
        var cm = (ConnectivityManager)GetSystemService(ConnectivityService);
        return cm.ActiveNetworkInfo == null ? false : cm.ActiveNetworkInfo.IsConnected;
    }

If I understood you correctly from this sentence: And how to refresh whole fragment after user switch-on the internet, You want to detect, whenever any changes in the connection status happens, Therefore you absolutely need to use broadcast receivers.

First of all you should implement a broadcast receiver with a simple Event named ConnectionStatusChanged as follows:

[BroadcastReceiver()]
public class NetworkStatusBroadcastReceiver : BroadcastReceiver
{

    public event EventHandler ConnectionStatusChanged;

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

Then in your activity (in OnCreate() method for example, It doesn’t matter) create an instance of that receiver and register it:

var _broadcastReceiver = new NetworkStatusBroadcastReceiver();
_broadcastReceiver.ConnectionStatusChanged += OnNetworkStatusChanged;
Application.Context.RegisterReceiver(_broadcastReceiver, 
new IntentFilter(ConnectivityManager.ConnectivityAction));

Here is the body of the event handler:

private void OnNetworkStatusChanged(object sender, EventArgs e)
    {
       if(IsOnline()){
        Toast.MakeText(this, "Network Activated", ToastLength.Short).Show();
        // refresh content fragment.
       }
    }

To cut the long story short, NetworkStatusBroadcastReceiver receives any change in the network status of the device and invokes the ConnectionStatusChanged (When user enables data traffic or WiFi connection), Then you catch that event and check for network status using IsOnline() method. Very simple.

Answer:

You can use the MVVMCross plugin : Connectivity

It wil expose a boolean

/// <summary>
/// Gets if there is an active internet connection
/// </summary>
bool IsConnected { get; }

and a delegate on change state

/// <summary>
/// Event handler when connection changes
/// </summary>
event ConnectivityChangedEventHandler ConnectivityChanged;