Maui backround services

Sergey 25 Reputation points
2023-07-16T17:19:19.83+00:00

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);
        }
    }


Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,236 Reputation points Volunteer Moderator
    2023-07-16T19:48:49.53+00:00

    They are called background task in ios

    https://developer.apple.com/documentation/backgroundtasks/choosing_background_strategies_for_your_app

    For windows you just create a thread, or if you need to run when app is not running you create a windows service.

    https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-7.0&tabs=visual-studio


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.