当应用程序被用户终止时,工作管理器不工作。为什么?
Work Manager not working when app is killed by the user. Why?
我想执行任务,即使在使用工作管理器终止应用程序之后也是如此。但是,应用被杀死后,任务并没有被执行。
workManager = WorkManager.getInstance();
Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
OneTimeWorkRequest saveData = new OneTimeWorkRequest.Builder(SaveDataWorker.class).setConstraints(constraints).setInitialDelay(10,TimeUnit.SECONDS).build();
workManager.enqueue(saveData);
我发现,工作管理器取决于设备制造商。在我的例子中,它是一个 miui 设备,它不允许工作管理器工作,以防应用程序被终止或重新启动。当我向应用程序提供 "autostart permission".
时,工作经理工作
工作管理器完全取决于制造商,一些制造商或者您也可以说带有库存 ROM 的设备允许工作管理器正常工作,但有些设备制造商 ("Chinese ROM's") 非常他们在清除后台应用程序时非常激进,甚至会杀死工作管理器,但是,Google 正试图通过与 OEM 的对话使工作管理器在所有设备上正常工作。
截至目前,如果你真的想 运行 在后台进行任何操作,你可以在小米和其他一些设备中打开自动启动选项,或者你也可以在通知托盘中显示一个通知,这会生成一个应用程序到前台的 运行。您可以检查该应用程序是否仍在后台 运行ning,如果没有,您可以重新启动它。
if (!isServiceRunning(this, MyService::class.java)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(Intent(this, MyService::class.java))
} else {
startService(Intent(this, MyService::class.java))
}
}
private fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean {
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val services = activityManager.getRunningServices(Integer.MAX_VALUE)
if (services != null) {
for (i in services.indices) {
if (serviceClass.name == services[i].service.className && services[i].pid != 0) {
return true
}
}
}
return false
}
val am = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val pi = PendingIntent.getBroadcast(
applicationContext,
34,
Intent(this, MyBroadcastReceiver::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pi)
最后在广播接收器中。
override fun onReceive(context: Context?, intent: Intent?) {
Handler(Looper.getMainLooper()).post {
if (!isServiceRunning(context!!, MyService::class.java)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(Intent(context, MyService::class.java))
} else {
context.startService(Intent(context, MyService::class.java))
}
}
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val pi = PendingIntent.getBroadcast(
context, 34, Intent(context, MyBroadcastReceiver::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pi)
}
}
我想执行任务,即使在使用工作管理器终止应用程序之后也是如此。但是,应用被杀死后,任务并没有被执行。
workManager = WorkManager.getInstance();
Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
OneTimeWorkRequest saveData = new OneTimeWorkRequest.Builder(SaveDataWorker.class).setConstraints(constraints).setInitialDelay(10,TimeUnit.SECONDS).build();
workManager.enqueue(saveData);
我发现,工作管理器取决于设备制造商。在我的例子中,它是一个 miui 设备,它不允许工作管理器工作,以防应用程序被终止或重新启动。当我向应用程序提供 "autostart permission".
时,工作经理工作工作管理器完全取决于制造商,一些制造商或者您也可以说带有库存 ROM 的设备允许工作管理器正常工作,但有些设备制造商 ("Chinese ROM's") 非常他们在清除后台应用程序时非常激进,甚至会杀死工作管理器,但是,Google 正试图通过与 OEM 的对话使工作管理器在所有设备上正常工作。
截至目前,如果你真的想 运行 在后台进行任何操作,你可以在小米和其他一些设备中打开自动启动选项,或者你也可以在通知托盘中显示一个通知,这会生成一个应用程序到前台的 运行。您可以检查该应用程序是否仍在后台 运行ning,如果没有,您可以重新启动它。
if (!isServiceRunning(this, MyService::class.java)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(Intent(this, MyService::class.java))
} else {
startService(Intent(this, MyService::class.java))
}
}
private fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean {
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val services = activityManager.getRunningServices(Integer.MAX_VALUE)
if (services != null) {
for (i in services.indices) {
if (serviceClass.name == services[i].service.className && services[i].pid != 0) {
return true
}
}
}
return false
}
val am = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val pi = PendingIntent.getBroadcast(
applicationContext,
34,
Intent(this, MyBroadcastReceiver::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pi)
最后在广播接收器中。
override fun onReceive(context: Context?, intent: Intent?) {
Handler(Looper.getMainLooper()).post {
if (!isServiceRunning(context!!, MyService::class.java)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(Intent(context, MyService::class.java))
} else {
context.startService(Intent(context, MyService::class.java))
}
}
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val pi = PendingIntent.getBroadcast(
context, 34, Intent(context, MyBroadcastReceiver::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pi)
}
}