简体   繁体   中英

Background service to check internet connectivity in Xamarin forms C#

I have implemented a function in Xamarin (C# based code) that check for internet connectivity. I have access this method called CheckInternet() through a Dependency Service in the PCL.

What I want to achieve is to implement a sort of Background Service that runs when the app is running to keep checking internet connectivity. My app detects a list of items and send it to a server if there is an internet connection. If no, the item color remains red on my screen.

And once the internet connectivity is back, the items that have not been sent to the server are automatically sent.

Can someone help me to achieve this background service in C# ?

I think you can take a look to this plugin

and this event

/// <summary>
/// Event handler when connection changes
/// </summary>
event ConnectivityChangedEventHandler ConnectivityChanged; 

CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
  {
    page.DisplayAlert("Connectivity Changed", "IsConnected: " + args.IsConnected.ToString(), "OK");
  };

In addition to @alessandro-caliaro answer; I believe what you need to look for is how to implement backgrounding in your apps. Its slightly tricky to get it working in forms projects, as it needs plaform-specific implementation.

These are two articles that talk about this concept in detail (and illustrate how to implement resilient network calls using backgrounding techniques for each platform and also, have shared logic at PCL level).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM