3

I'm trying to setup a WorkManager that executes a task every 15 minutes. For now I'm doing this test adding an entry in a firebase database.

I'm using the nuget package Xamarin.AndroidX.Work.Runtime (2.5.0.2)

This works fine if the app is in foreground or background, but if I kill the app it stops working.

I'm doing this test with a Nokia 8 Sirocco (api 28) and with a Samsung Galaxy A21S (api 30) that both mount Android Stock.

This is how I launch the work:

private void StartParallelWorkManager(int minutes)
    {
        PeriodicWorkRequest taskLauncher = PeriodicWorkRequest.Builder.From<UniquePeriodicWorker>(TimeSpan.FromMinutes(minutes)).Build();
        WorkManager.GetInstance(Xamarin.Essentials.Platform.AppContext).EnqueueUniquePeriodicWork("TestTask", ExistingPeriodicWorkPolicy.Keep, taskLauncher);
    }

and this is my Worker class:

public class UniquePeriodicWorker : Worker
    {
        public UniquePeriodicWorker(Context context, WorkerParameters workerParameters) : base(context, workerParameters)
        {
        }

        public override Result DoWork()
        {
            
            System.Console.WriteLine("{0} DO WORK", DateTime.Now.ToString("dd/MM HH:mm:ss"));
            try
            {
                _ = CrossCloudFirestore.Current
                             .Instance
                             .Collection("WorkManagerRuns")
                             .AddAsync<DateTime>(DateTime.Now);
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
            return Result.InvokeSuccess();
        }
    }

Doing the same test with native code in android studio works like a charm.

Am I missing something in Xamarin side or is Xamarin.AndroidX.Work.Runtime bugged? Any suggestion?

  • I search one thread about [workmanager](https://stackoverflow.com/questions/55349488/work-manager-not-working-when-app-is-killed-by-the-user-why) , it said that the work manager depends on the device manufacturer. – Cherry Bu - MSFT Jul 05 '21 at 07:14
  • hello @CherryBu-MSFT, yes it depends on the manufacturer. Some Chinese manufacturer mounting Android MIUI have this problem. Anyway I'm doing this test with Android Stock that doesn't have this problem. In fact doing the same exact test with Android Studio (using Kotlin) works perfectly. My problem is that I have to do it in a already existent Xamarin app. – Claudio Pisanu Jul 05 '21 at 15:36
  • I test [workmanager](https://devblogs.microsoft.com/xamarin/getting-started-workmanager/) in android emulator, it doesn't work if I kill app. – Cherry Bu - MSFT Jul 06 '21 at 06:36

0 Answers0