ROHIT MANDALAPU
ROHIT MANDALAPU

Reputation: 438

How to check internet connection continuously in Xamarin.Android Native?

My application is completely based on internet and it does not work without it, when the internet is not available or it is slow my application is getting stopped unfortunately. I tried to implement try, catch but it is not helping me out as it is not throwing any exception, then I thought that I have to check the internet connectivity continuously till the app is running and stop any activity from performing and set a popup to connect to the internet.

I am able to get the popup whenever I call a method which has the following code inside it,

 ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
                NetworkInfo networkInfo = connectivityManager.ActiveNetworkInfo;
                if (networkInfo == null)
                {
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    alert.SetTitle("Network");
                    alert.SetMessage("Please turn of your Wifi or Mobile Data From Settings");
                    alert.SetPositiveButton("OK", (senderAlert, args) => {
                        Intent intent = new Intent(Android.Provider.Settings.ActionSettings);
                        StartActivity(intent);
                    });

                    alert.SetNegativeButton("Cancel", (senderAlert, args) => {
                        Toast.MakeText(this, "Cancelled!", ToastLength.Short).Show();
                        Finish();
                    });

                    Dialog dialog = alert.Create();
                    dialog.Show();

But I am unable to get the connection checked continuously, So Can some one Please help me to complete get this functionality in my application.

Upvotes: 3

Views: 1922

Answers (1)

Jesus Angulo
Jesus Angulo

Reputation: 2666

You need to use a BroadcastReceiver to monitor network changes.

    [BroadcastReceiver(Enabled = true)]
    [IntentFilter(new[] { "android.net.conn.CONNECTIVITY_CHANGE" })]
    [Android.Runtime.Preserve(AllMembers = true)]
    public class ConnectivityChangeBroadcastReceiver : BroadcastReceiver
    {

        public override void OnReceive(Context context, Intent intent)
        {
            if (intent.Action != ConnectivityManager.ConnectivityAction)
                return;

             //Check if is connected and raise a custom event or store 
             //the current in a static global variable

        }
    }
}

In Android 7.0, you need to remove IntentFilter from the class and register the receiver.

var receiver = new ConnectivityChangeBroadcastReceiver();
Application.Context.RegisterReceiver(receiver, new IntentFilter(ConnectivityManager.ConnectivityAction));

Another option is to use the ConnectivityPlugin https://github.com/jamesmontemagno/ConnectivityPlugin , which is easier to use.

CrossConnectivity.Current.ConnectivityChanged += HandleConnectivityChanged;


void HandleConnectivityChanged (object sender, ConnectivityChangedEventArgs e)
{
    // You can check the network status in 
    //e.IsConnected
}

Note that The ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE permissions are required.

Upvotes: 1

Related Questions