Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always Check if there is Internet Connection Xamarin forms

I am doing a xamarin forms app, i want to check every second if there is a internet connection, if the connection is lost, the program should go to diferent page. I am using the plugin "Xam.Plugin.Connectivity" , but doesnt do what i want. Is possible to do what i want?

like image 258
Phill Avatar asked Oct 20 '17 15:10

Phill


People also ask

How to Check Connectivity in Xamarin forms?

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.

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.

How do I close xamarin app?

For closing the App I wrote the following code: Application. Current. Quit();


2 Answers

Edit: this can be easily done with the new Xamarin Essentials Connectivity plugin, just follow the instructions there :D

Create a method in your App.cs (or App.xaml.cs) like this:

private async void CheckConnection()
{
    if(!CrossConnectivity.Current.IsConnected)
         await Navigation.PushAsync(new YourPageWhenThereIsNoConnection());
    else
         return;
}
    

And use it on your main app method like this:

public App()
{
    InitializeComponent();

    var seconds = TimeSpan.FromSeconds(1);
    Xamarin.Forms.Device.StartTimer(seconds,
        () =>
        {
             CheckConnection();
        });
}
like image 134
FabriBertani Avatar answered Sep 20 '22 16:09

FabriBertani


Never used, but this is a documentation about the plugin you are using

Detecting Connectivity Changes

Often you may need to notify your user or respond based on network changes. You can do this by subscribing several different events.

Changes in Connectivity

When any network connectivity is gained, changed, or loss you can register for an event to fire:

/// <summary>
/// Event handler when connection changes
/// </summary>
event ConnectivityChangedEventHandler ConnectivityChanged; 
You will get a ConnectivityChangeEventArgs with the status if you are connected or not:

public class ConnectivityChangedEventArgs : EventArgs
{
  public bool IsConnected { get; set; }
}

public delegate void ConnectivityChangedEventHandler(object sender, ConnectivityChangedEventArgs e);
CrossConnectivity.Current.ConnectivityChanged += async (sender, args) =>
  {
      Debug.WriteLine($"Connectivity changed to {args.IsConnected}");
  };

Changes in Connectivity Type

When any network connectivity type is changed this event is triggered. Often it also is accompanied by a ConnectivityChanged event.

/// <summary>
/// Event handler when connection type changes
/// </summary>
event ConnectivityTypeChangedEventHandler ConnectivityTypeChanged;
When this occurs an event will be triggered with EventArgs that have the most recent information:

public class ConnectivityTypeChangedEventArgs : EventArgs
{
    public bool IsConnected { get; set; }
    public IEnumerable<ConnectionType> ConnectionTypes { get; set; }
}
public delegate void ConnectivityTypeChangedEventHandler(object sender, ConnectivityTypeChangedEventArgs e);
Example:

CrossConnectivity.Current.ConnectivityTypeChanged += async (sender, args) =>
  {
      Debug.WriteLine($"Connectivity changed to {args.IsConnected}");
      foreach(var t in args.ConnectionTypes)
        Debug.WriteLine($"Connection Type {t}");
  };
like image 39
Alessandro Caliaro Avatar answered Sep 19 '22 16:09

Alessandro Caliaro