You*_*ung 5 android android-6.0-marshmallow android-7.0-nougat android-8.0-oreo android-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)
有什么不对?
JobScheduler 仅在 Doze 模式下的维护间隔内执行作业。这样做是为了节省用户的电池。所以你所看到的都是预期的。
有关更多信息,请阅读有关 Doze 模式的文档。 https://developer.android.com/training/monitoring-device-state/doze-standby
| 归档时间: |
|
| 查看次数: |
2054 次 |
| 最近记录: |