任务终止后,Android工作管理器计划的工作人员丢失 [英] Android workmanager scheduled worker lost after task killed

本文介绍了任务终止后,Android工作管理器计划的工作人员丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的 WorkManager API 1.0.0-alpha06 每 15 分钟运行一次工作程序.如果我没有错,将工作管理器与 PeriodicWorkRequest 一起使用应该会使工作人员的任务终止并重新启动电话,但是当我从最近的应用程序"中滑动任务时,预定的工作人员丢失了(我已经等了大约 45 分钟,以查看安排在 15 分钟间隔内的工作人员的任何日志).

I'm trying to make a worker-run every 15 minutes using the new WorkManager API, 1.0.0-alpha06. If I'm not wrong, using Work manager with PeriodicWorkRequest should make the worker outlive task kills and phone reboots, but when I swipe the task from the Recent Apps the scheduled worker is lost (I've waited for around 45 minutes to see any logs of the worker scheduled for 15 minutes interval).

这些是我的文件:

MyExampleWorker.java:

MyExampleWorker.java:

public class MyExampleWorker extends Worker{
    public static final String TAG = "MY_EXAMPLE_WORKER";

    @NonNull
    @Override
    public Result doWork() { 
        Log.i(TAG, "Starting background worker");
        // Getting configuration data
        long param1 = getInputData().getLong("param1", 60000);
        String param2 = getInputData().getString("param2");
        String param3 = getInputData().getString("param3");

        PackageManager pckMgr = mContext.getPackageManager();
        ...
        ..
        .
        return Result.SUCCESS;
    }

}

Main.java:此方法会在应用启动后立即触发

Main.java: this method fires as soon as the app is launched

@ReactMethod
public void execute() {
    Log.i(TAG, "inside execute, setting up periodic worker in workManager");

    Data inputData = new Data.Builder()
            .putLong("param1", 60000)
            .putString("param2", "something")
            .putString("param3", "something else")
            .build();

    PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest
            .Builder(MyExampleWorker.class, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS)
            .setInputData(inputData)
            .addTag(MyExampleWorker.TAG)
            .build();

    WorkManager.getInstance().enqueueUniquePeriodicWork(MyExampleWorker.TAG, ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest);
}

更新:不仅如此,如果我再次打开应用程序,我会看到 inside execute, setting up a period of worker in workManager" 的日志,但似乎没有安排工作人员,已经一个多小时了并且 logcat 中没有工作人员的日志.

UPDATE: Not only that, but if I open the app once again I see the log for "inside execute, setting up a periodic worker in workManager" but seems the worker is not scheduled, it has been over an hour and no logs for the worker are present in logcat.

我在这里遗漏了什么吗?

Am I missing something here?

非常感谢任何帮助!!

迭戈

推荐答案

这是由于电池优化和/或Doze模式引起的问题,尤其是在您使用Oxygen OS、MIUI等中文ROM时发生.

This is the problem caused by Battery Optimization and/or Doze mode, occurs especially when you are using Chinese Rom like Oxygen OS, MIUI etc.

在 Stock Rom 上测试您的应用,这将非常有效.根本不会有问题.这是因为这些中文 ROM 启用了他们自定义的节电技术,以防止任何后台服务运行.

Test your app on Stock Rom's, this will work perfectly fine. There would be no issues at all. This is because these Chinese ROM enable their custom power saving techniques that prevent any background services from running.

你可以做两件事:

  1. 使用忽略电池优化并将您的应用列入白名单.启用此功能后,系统会询问用户,

您可以通过编程方式询问

You can ask programmatically

    Intent intent = new Intent();
    String packageName = context.getPackageName();
    PowerManager pm = (PowerManager) 
    context.getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName))
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {
      intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
      intent.setData(Uri.parse("package:" + packageName));
    }
    context.startActivity(intent);

在你的清单中

<uses-permission 
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

通过这种方式,您可以将您的应用列入白名单,并且工作经理将起作用,但不幸的是,如果您请求上述许可,Google 将从他们的 Play 商店中删除您的应用.没有人知道为什么!!!

This way you can whitelist your app and work manager will work, but unfortunately, Google will remove your app from their play store if you request the above permission. No one knows why!!!

阅读这篇关于 Google 反信任问题的文章

  1. 第二种方法是,您可以通过不显示上述对话框来要求用户将他们的应用列入白名单.你可以这样做

startActivityForResult(new Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0);

这篇关于任务终止后,Android工作管理器计划的工作人员丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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