小编You*_*ung的帖子

在三星 Galaxy 手机上,手机进入打瞌睡模式后 WorkManager 被取消

我正在尝试使用 WorkManager 运行后台服务。当应用程序进入后台时,我启动了 WorkManager。每当 WorkManger 运行时,我也会启动后台服务。应用程序进入后台后,我可以看到我的应用程序服务。我还可以看到,它是在我通过在应用程序列表中滑动关闭应用程序后大约 15 分钟后启动的。所以我确信 Workmanager 在开始时正在工作。但我认为它不适用于打瞌睡模式。将手机从打瞌睡模式唤醒后,我也看不到我的服务。

我的代码:

public class MyWorker extends Worker {

  public MyWorker() {
    super();
  }

  @NonNull
  @Override
  public WorkerResult doWork() {

    Intent intent = new Intent(getApplicationContext(), MyService.class);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        MyJobIntentService.enqueueWork(getApplicationContext(), new Intent());
    } else {
Intent intent = new Intent(getApplicationContext(), MyService.class);
        getApplicationContext().startService(intent);
    }

    return WorkerResult.SUCCESS;
  }
}

public void startWorkManger() {

  PeriodicWorkRequest request = new PeriodicWorkRequest
         .Builder(MyWorker.class, MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS).addTag(TAG).build();
  mWorkManager.enqueue(request);
}
Run Code Online (Sandbox Code Playgroud)

有什么不对?

android android-6.0-marshmallow android-7.0-nougat android-8.0-oreo android-workmanager

5
推荐指数
1
解决办法
2054
查看次数