dotnet / dotnet/android-libraries

WorkManager not working when app is killed

Open
#333 16 comments 4 reactions 1 assignee Claimed by @moljac View on GitHub
Dominant language
C#
Stars
317
Forks
73
Avg merge
1d 8h
Merged PRs (30d)
17

Description

### Version Information

- Visual Studio version: 8.10.4 (build 11)
- Xamarin.Android version: 11.3.0.4 (Visual Studio Community)
- Using AndroidX or Support Libraries: Xamarin.AndroidX.Work.Runtime (2.5.0.2)

### Describe your Issue:

Im 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(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.Now);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
return Result.InvokeSuccess();
}
}
```

**Doing the same test in android studio (using Kotlin) works like a charm.**

Am I missing something in Xamarin side or Xamarin.AndroidX.Work.Runtime is not capable to achieve the same behavior of the native equivalent??

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.