They are called background task in ios
For windows you just create a thread, or if you need to run when app is not running you create a windows service.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
What is the proper way to add it for ios and windows platforms using .net 6 ?
I wrote android background service which tracks internet connection and sync data with server when it turns on.
But i can't find a way to achive it for ios and windows. Can anyone propose proper way to do it? Official doccumentation does not cover it, in every articale just scrapes of information. Thanks in advance for any help.
Adnroid service looks following way:
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
App.UIParent = this;
StartService(new Intent(this, typeof(SynchronizationBackgroundService)));
}
}
[Service]
public class SynchronizationBackgroundService : Service
{
private ConnectivityMonitor _connectivityMonitor;
public override IBinder OnBind(Intent intent) {
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) {
_connectivityMonitor = new ConnectivityMonitor(this);
_connectivityMonitor.ConnectivityChanged += ConnectivityMonitor_ConnectivityChanged;
_connectivityMonitor.StartMonitoring();
return StartCommandResult.Sticky;
}
public override void OnDestroy() {
_connectivityMonitor.StopMonitoring();
_connectivityMonitor.ConnectivityChanged -= ConnectivityMonitor_ConnectivityChanged;
base.OnDestroy();
}
private void ConnectivityMonitor_ConnectivityChanged(object sender, bool isConnected) {
if (isConnected) {
var synchronizationService = MauiApplication.Current.Services.GetService(typeof(ISynchronizationService)) as ISynchronizationService;
Task.Run(async () =>
{
await synchronizationService.UploadAsync();
});
}
}
}
public class ConnectivityMonitor : BroadcastReceiver
{
public event EventHandler<Boolean> ConnectivityChanged;
private Context _context;
public ConnectivityMonitor(Context context) {
_context = context;
}
public void StartMonitoring() {
var intentFilter = new IntentFilter(ConnectivityManager.ConnectivityAction);
_context.RegisterReceiver(this, intentFilter);
}
public void StopMonitoring() {
_context.UnregisterReceiver(this);
}
public override void OnReceive(Context context, Intent intent) {
var connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
var activeNetwork = connectivityManager.ActiveNetworkInfo;
var isConnected = activeNetwork != null && activeNetwork.IsConnected;
ConnectivityChanged?.Invoke(this, isConnected);
}
}
They are called background task in ios
For windows you just create a thread, or if you need to run when app is not running you create a windows service.