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

Viewed 1711

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.

1 Answers
Related