关闭的应用程序如何在 Android 10 上使用 WorkManager 执行定期工作? [英] How can a closed application perform periodic work with WorkManager on Android 10?

查看:48
本文介绍了关闭的应用程序如何在 Android 10 上使用 WorkManager 执行定期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问了一个关于定期工作的问题:如何才能关闭的应用程序接收定期广播?.答案是使用 WorkManager,这是 Android 9 上的一个很好的解决方案.

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.

在另一台使用 Android 10 的设备上,前一个问题的解决方案不再适用.这似乎是一个普遍的问题.这里的问题已经得到了很多支持,但它的单一答案没有被接受,也没有帮助我:当应用在 Android 10 中被杀死时,WorkManager 无法工作,尽管在版本 9(Pie)之前工作正常.

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).

因此我想为Android 10制定一个具体问题.有谁知道如何解决它?

Therefore I would like to formulate a specific problem for Android 10. Does anyone know how to solve it?

  1. 在 AndroidStudio 中使用 File->New->New Project...-> 创建一个空活动空活动->语言:Java,SDK:API 28.
  2. 添加一个包含以下内容的类 MyWorker:

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. 修改MainActivity类,使其内容如下:
  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. 安装并运行应用程序.播放来自 MyWorker::doWork 的提示音并显示相应的 Toast.
  2. 现在向上滑动活动以关闭它.
  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.

期望:应该像在 Android 9 上一样继续执行定期工作.

Expectation: The periodic work should continue to be executed like it has been on Android 9.

实际行为:不再发生任何事情.WorkManager 显然已停止.

Actual behaviour: Nothing happens anymore. The WorkManager has obviously been stopped.

问题:如何修改示例以在 Android 10 上运行?

Question: How can I modify the example to work on Android 10?

推荐答案

代码或项目设置没有任何问题.我的设备一直处于省电模式,这显然阻止了活动关闭时的周期性 WorkManager 任务.退出省电模式解决了问题.

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.

这篇关于关闭的应用程序如何在 Android 10 上使用 WorkManager 执行定期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆