【问题标题】:Work Manager not working when app is killed by the user. Why?当应用被用户杀死时,工作管理器不工作。为什么?
【发布时间】:2019-08-16 09:13:17
【问题描述】:

我想执行一个任务,即使在应用程序被工作管理器杀死之后。但是,应用被杀死后,任务并没有被执行。

    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);

【问题讨论】:

  • 但是,应用被杀死后任务并没有被执行。它不应该被执行!
  • this 链接可能有帮助,试一试。
  • @Ibrahim Ali,应用被杀后如何执行任务?
  • 你可以启动一个永无止境的后台服务,看看这个link

标签: android android-workmanager


【解决方案1】:

我发现,工作经理取决于设备制造商。在我的情况下,它是一个 miui 设备,如果应用程序被杀死或重新启动,它不允许工作管理器工作。当我为应用程序提供“自动启动权限”时,工作经理开始工作。

【讨论】:

  • 只是一个问题:有没有办法知道工作经理是否可以在应用程序被杀死的情况下工作?那是在入队之前?
【解决方案2】:

工作经理完全取决于制造商,一些制造商或者您也可以说具有库存ROM的设备允许工作经理正常工作,但是有些设备制造商(“中国ROM”)在清除时非常激进后台应用程序,它们甚至杀死了工作管理器,但是,Google 正试图通过与 OEM 的对话使工作管理器在所有设备上正常工作。

到目前为止,如果您真的想在后台运行任何东西,您可以在小米和其他一些设备中打开自动启动选项,或者您也可以在通知托盘中显示通知,使应用程序在前台运行。您可以检查应用程序是否仍在后台运行,如果没有,您可以重新启动它。

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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多