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?