【问题标题】:WorkManager is cancelled after phone goes into doze mode on Samsung Galaxy phones在三星 Galaxy 手机上进入打盹模式后,WorkManager 被取消
【发布时间】:2018-06-08 22:03:59
【问题描述】:

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

有什么问题吗?

【问题讨论】:

  • 您找到解决方案了吗?如果是,请在此处提及以帮助其他人。

标签: android android-6.0-marshmallow android-7.0-nougat android-8.0-oreo android-workmanager


【解决方案1】:

JobScheduler 在打盹模式下只会在维护间隔内执行作业。这样做是为了节省用户的电池电量。所以你看到的是预期的。

有关更多信息,请阅读有关打盹模式的文档。 https://developer.android.com/training/monitoring-device-state/doze-standby

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-07
    • 2015-10-01
    • 2016-04-19
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多