Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether Internet is on or off in Xamarin Android

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 !

like image 342
Dhruv Gohil Avatar asked Oct 26 '15 10:10

Dhruv Gohil


People also ask

How do you check if the Internet is on or off in Android?

Open your Settings app and tap Network & internet or Connections. Depending on your device, these options may be different. Turn Wi-Fi off and mobile data on, and check if there's a difference. If not, turn mobile data off and Wi-Fi on and check again.

How do I check my connectivity in xamarin?

Open the AndroidManifest. xml file under the Properties folder and add the following inside of the manifest node. Or right click on the Android project and open the project's properties. Under Android Manifest find the Required permissions: area and check the Access Network State permission.

How can I check my internet status?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top.

What is xamarin essentials?

Xamarin. Essentials provides a single cross-platform API that works with any iOS, Android, or UWP application that can be accessed from shared code no matter how the user interface is created. See the platform & feature support guide for more information on supported operating systems.


2 Answers

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; 
like image 28
LDut Avatar answered Oct 05 '22 23:10

LDut


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.

like image 165
a.toraby Avatar answered Oct 06 '22 01:10

a.toraby