【发布时间】: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