Check whether Internet is on or off in Xamarin Android
Asked Answered
S

3

9

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 !

Silver answered 26/10, 2015 at 10:5 Comment(1)
search before ask. see thisSchwing
A
0

Try this :

NetworkStatus internetStatus = Reachability.InternetConnectionStatus();

if(!Reachability.IsHostReachable("http://google.com")) {
    // Put alternative content/message here
}
else
{
    // Put Internet Required Code here
}
Abdicate answered 26/10, 2015 at 10:17 Comment(1)
Reachability class doesn't exist in the current context!Owsley
S
13

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.

Sough answered 27/10, 2015 at 5:0 Comment(1)
the Compiler Says new IntentFilter(ConnectivityManager.ConnectivityAction)); is depricated!Morality
P
4

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; 
Pharmacopsychosis answered 29/10, 2015 at 12:38 Comment(2)
no tutorial on how to try it for someone who is a beginner in xamarin.Demetria
Above connectivity library is good, but it had several device compatibility problem, you need to make sure it is supporting well on other devices.Edwinaedwine
A
0

Try this :

NetworkStatus internetStatus = Reachability.InternetConnectionStatus();

if(!Reachability.IsHostReachable("http://google.com")) {
    // Put alternative content/message here
}
else
{
    // Put Internet Required Code here
}
Abdicate answered 26/10, 2015 at 10:17 Comment(1)
Reachability class doesn't exist in the current context!Owsley

© 2022 - 2024 — McMap. All rights reserved.