Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当应用程序被用户杀死时,工作管理器不工作。为什么?_Android_Android Workmanager - Fatal编程技术网

Android 当应用程序被用户杀死时,工作管理器不工作。为什么?

Android 当应用程序被用户杀死时,工作管理器不工作。为什么?,android,android-workmanager,Android,Android Workmanager,我想执行一项任务,即使在使用work manager终止应用程序之后也是如此。但是,该任务在应用程序被终止后不会执行 workManager = WorkManager.getInstance(); Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build(); OneTimeWorkRequest saveData = n

我想执行一项任务,即使在使用work manager终止应用程序之后也是如此。但是,该任务在应用程序被终止后不会执行

    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设备,如果应用程序被终止或重新启动,它将不允许work manager工作。当我向应用程序提供“autostart许可”时,工作经理工作了。

工作经理完全取决于制造商,一些制造商或您也可以说带有库存ROM的设备允许工作经理按其应有的方式工作,但也有一些设备制造商(“中文ROM”)他们在清除后台应用程序时非常激进,他们甚至杀死了工作经理,然而,谷歌正试图通过与OEM的对话使工作经理在所有设备上正常工作

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

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)

但是,该任务在应用程序被终止后不会执行。它不应该被执行!链接可能会有帮助,试试看。@Ibrahim Ali,即使在应用程序被杀死后如何执行任务?您可以启动一个永无止境的后台服务,签出此链接。
 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)
    }
}