0

Implemented WorkManager using the following code.
It is configured to perform background tasks every 30 minutes. .
However, after running successfully for approximately 5-6 hours, it fails to trigger at the specified interval and stops functioning altogether..
Are there any issues with the code? Am I missing any configurations?.

using Android.App.Job;
using Android.Content
using Android.Runtime;
using AndroidX.Work;
public class BackgroundWork : Worker
{
    public BackgroundWork(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }

        public BackgroundWork(Context context, WorkerParameters workerParams) : base(context, workerParams)
        {
        }
        public override Result DoWork()
        {
            //Analytics.TrackEvent("DoWork called.."+ DeviceInfo.Name);
            bool bResult = AlarmHelper.SetAlarms();

            if (bResult)
                return Result.InvokeSuccess();
            else
                return Result.InvokeFailure();
        }
        
        public override void OnStopped()
        {
            //Logger.LogEvent("BackgroundWork OnStopped");
            base.OnStopped();
        }
}



using AndroidX.Work;
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        
#if __ANDROID__
        using var builder = new Constraints.Builder();
        builder.SetRequiredNetworkType(AndroidX.Work.NetworkType.Connected);
        var workConstraints = builder.Build();
        var interval = TimeSpan.FromMinutes(30);

        SetupWorker<BackgroundWork>(interval, workConstraints);
#endif
    }
    
    private void SetupWorker<TWorker>(TimeSpan interval, Constraints workConstraints) where TWorker : Worker
    {
        var request = PeriodicWorkRequest.Builder.From<TWorker>(interval)
            .SetConstraints(workConstraints)
            .Build();
        WorkManager.GetInstance(ApplicationContext)
            .EnqueueUniquePeriodicWork(nameof(TWorker), ExistingPeriodicWorkPolicy.Replace, request);        
    }
}

Below are the timing logs for reference.

Starting logging at 6/12/2023 6:44:25 PM
6/12/2023 6:44:28 PM : Bday Reminder Start.
6/12/2023 6:44:32 PM :      Done -- Reminders.0
6/12/2023 6:45:16 PM : Today Reminder Start.
6/12/2023 6:45:22 PM :   Done -- Reminders.0
6/12/2023 7:09:32 PM : PopulateDayChange -- called -> 6/12/2023 7:09:31 PM
6/13/2023 12:58:15 PM : PopulateDayChange -- called -> 6/13/2023 12:58:15 PM
6/13/2023 12:58:15 PM : Populate Day Change -- day change happened -> 6/13/2023 12:58:15 PM
6/13/2023 12:58:15 PM : Populate Day Change -- day change happened -> 6/13/2023 12:58:15 PM
6/13/2023 12:58:24 PM :          --- Done -- 6/13/2023 12:58:24 PM
6/13/2023 6:42:00 PM : PopulateDayChange -- called -> 6/13/2023 6:41:59 PM
6/13/2023 6:42:00 PM : PopulateDayChange -- called -> 6/13/2023 6:41:59 PM
6/13/2023 6:44:05 PM : PopulateDayChange -- called -> 6/13/2023 6:44:05 PM
6/13/2023 6:45:00 PM : Today Reminder Start.
6/13/2023 6:45:00 PM :   Done -- Reminders.2
6/13/2023 6:47:59 PM : PopulateDayChange -- called -> 6/13/2023 6:47:59 PM
6/13/2023 6:50:30 PM : PopulateDayChange -- called -> 6/13/2023 6:50:30 PM
6/14/2023 3:20:46 PM : PopulateDayChange -- called -> 6/14/2023 3:20:46 PM
6/14/2023 3:20:46 PM : Populate Day Change -- day change happened -> 6/14/2023 3:20:46 PM
6/14/2023 3:20:46 PM : Populate Day Change -- day change happened -> 6/14/2023 3:20:46 PM
6/14/2023 3:20:58 PM :          --- Done -- 6/14/2023 3:20:58 PM
6/14/2023 3:50:47 PM : PopulateDayChange -- called -> 6/14/2023 3:50:47 PM
6/14/2023 4:20:47 PM : PopulateDayChange -- called -> 6/14/2023 4:20:47 PM
6/14/2023 4:50:48 PM : PopulateDayChange -- called -> 6/14/2023 4:50:48 PM
6/14/2023 5:20:48 PM : PopulateDayChange -- called -> 6/14/2023 5:20:48 PM
6/14/2023 5:50:48 PM : PopulateDayChange -- called -> 6/14/2023 5:50:48 PM
6/14/2023 6:20:48 PM : PopulateDayChange -- called -> 6/14/2023 6:20:48 PM
6/14/2023 6:44:00 PM : Bday Reminder Start.
6/14/2023 6:44:01 PM :      Done -- Reminders.0
6/14/2023 6:45:00 PM : Today Reminder Start.
6/14/2023 6:45:01 PM :   Done -- Reminders.2
6/14/2023 6:50:49 PM : PopulateDayChange -- called -> 6/14/2023 6:50:49 PM
6/14/2023 7:20:49 PM : PopulateDayChange -- called -> 6/14/2023 7:20:49 PM
6/15/2023 11:08:06 AM : PopulateDayChange -- called -> 6/15/2023 11:08:06 AM
6/15/2023 11:08:06 AM : Populate Day Change -- day change happened -> 6/15/2023 11:08:06 AM
6/15/2023 11:08:08 AM :          --- Done -- 6/15/2023 11:08:08 AM
6/15/2023 11:38:08 AM : PopulateDayChange -- called -> 6/15/2023 11:38:08 AM
6/15/2023 12:08:08 PM : PopulateDayChange -- called -> 6/15/2023 12:08:08 PM
6/15/2023 12:47:09 PM : PopulateDayChange -- called -> 6/15/2023 12:47:09 PM
6/15/2023 1:17:09 PM : PopulateDayChange -- called -> 6/15/2023 1:17:09 PM
Sunil Parmar
  • 1,223
  • 1
  • 13
  • 22
  • Does this answer make help? You can refer to [How to get WorkManager always running in background](https://stackoverflow.com/questions/51574716/how-to-get-workmanager-always-running-in-background). – Guangyu Bai - MSFT Jun 16 '23 at 03:11
  • @GuangyuBai-MSFT, Thank you for reply, This link does not help. – Sunil Parmar Jun 16 '23 at 08:40
  • Are you aware of Android [doze mode & app standby](https://developer.android.com/training/monitoring-device-state/doze-standby)? – Trevor Balcom Jun 16 '23 at 13:40
  • This problem is not specific to Maui. It is quite difficult on Android to get background work to run repeatedly, forever. There are numerous discussions of this topic. Search `android WorkManager not working after few hours`. – ToolmakerSteve Jun 16 '23 at 21:01
  • @ToolmakerSteve thank you for reply. Could you please provide a viable solution? WhatsApp currently checks for new messages periodically, after every few hours. – Sunil Parmar Jun 20 '23 at 06:34
  • @TrevorBalcom thank you for reply. Yes I am aware of doze mode. – Sunil Parmar Jun 20 '23 at 06:37
  • 1
    From the docs: Periodically, the system exits Doze for a brief time to let apps complete their deferred activities. During this maintenance window, the system runs all pending syncs, jobs, and alarms, and lets apps access the network. – Trevor Balcom Jun 20 '23 at 13:45
  • Users can manually add an exception for your app. You can wake a device from doze mode with a high priority FCM push notification. Known apps like WhatsApp are frequently whitelisted. – Trevor Balcom Jun 20 '23 at 13:50

0 Answers0