I have asked a question about periodic work here: How can a closed application receive periodic broadcasts?. The answer has been to use WorkManager
and this has been a great solution on Android 9.
On another device using Android 10 the solution of this former question does not work anymore. This seems to be a common problem. The question here has been upvoted a lot, but its single answer is not accepted and it also didn't help me: WorkManager not working when app killed in Android 10 although working fine till version 9 (Pie).
Therefore I would like to formulate a specific problem for Android 10. Does anyone know how to solve it?
- Create an empty activity in AndroidStudio with
File
->New
->New Project...
->Empty Activity
->Language: Java, SDK: API 28
. - Add a class MyWorker with the following content:
package org.test.myapplication;
import android.content.Context;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
public class MyWorker extends Worker
{
public MyWorker(@NonNull Context context, @NonNull WorkerParameters params)
{
super(context, params);
}
@Override
public Result doWork()
{
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
// Play tone to show that the worker is working
ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 200);
toneGenerator.startTone(ToneGenerator.TONE_CDMA_EMERGENCY_RINGBACK, 1000);
// Also display some message
Toast.makeText(getApplicationContext(), "Testing", Toast.LENGTH_SHORT).show();
}
}, 1000);
// Indicate whether the work finished successfully with the Result
return Result.success();
}
}
- Modify the class
MainActivity
to have the following content:
package org.test.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WorkRequest workRequest =
new PeriodicWorkRequest.Builder(MyWorker.class, 15, TimeUnit.MINUTES).build();
WorkManager.getInstance(this).enqueue(workRequest);
}
}
- Install and run the application. The tone from
MyWorker::doWork
is played and the respective toast is displayed. - Now swipe the activity up to close it.
Expectation: The periodic work should continue to be executed like it has been on Android 9.
Actual behaviour: Nothing happens anymore. The WorkManager
has obviously been stopped.
Question: How can I modify the example to work on Android 10?
Handler
might be GC'd before the time elapses, might have problems from trying to display aToast
without anActivity
, etc.). Use Logcat to log a message before returningResult.success()
. – PaunchWorkManager
is not guaranteed to keep your process around to be able to receive broadcasts. My reference to polling usingWorkManager
was not to receive broadcasts, but to poll for some system state. – PaunchWorkManager
cannot reliably play tones when the activity is closed, what can I use instead? – VelizdoWork()
is already running on a background thread, you would not needHandler
. I do not know why your example is using aHandler
or playing tones. Use Logcat. "So when my WorkManager cannot reliably play tones when the activity is closed, what can I use instead?" -- I would start by using Logcat instead of playing tones. Then, I would make sure that I am on the latest production version of theWorkManager
library. – PaunchdoWork()
getting called every 15 minutes after the task removal, create a project that reproduces those findings and file a bug report. In the end, evenWorkManager
will be unreliable on some devices. A Samsung is fairly mainstream, though, and so the team behindWorkManager
should be, um, working to get it to be reliable there. – Paunch