How can a closed application perform periodic work with WorkManager on Android 10?

岁酱吖の 提交于 2021-02-11 12:47:33

问题


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?

  1. Create an empty activity in AndroidStudio with File->New->New Project...->Empty Activity->Language: Java, SDK: API 28.
  2. 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();
    }
}
  1. 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);
    }
}
  1. Install and run the application. The tone from MyWorker::doWork is played and the respective toast is displayed.
  2. 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?


回答1:


There has been nothing wrong with the code or the project setup. My device has been in power saving mode and this obviously has prevented the periodic WorkManager tasks when the activity had been closed. Leaving power saving mode has solved the problem.



来源:https://stackoverflow.com/questions/66030482/how-can-a-closed-application-perform-periodic-work-with-workmanager-on-android-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!