任务杀死后,Android Workmanager计划的工作人员丢失 [英] Android workmanager scheduled worker lost after task killed

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

问题描述

我正在尝试使用新的 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);
}

更新:不仅如此,如果我再次打开该应用程序,还会看到在内部执行,在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.

我在这里想念东西吗?

我们非常感谢您的帮助!

Any help is GREATLY appreciated!!

迭戈

推荐答案

这是由电池优化和/或打ze模式引起的问题,尤其是当您使用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 Workmanager计划的工作人员丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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