在android版本8.0中,我使用work manager定期启动广播,但当我从最近的任务中清除应用程序时,它不工作

在android版本8.0中,我使用work manager定期启动广播,但当我从最近的任务中清除应用程序时,它不工作,android,android-service,android-lifecycle,android-jetpack,android-workmanager,Android,Android Service,Android Lifecycle,Android Jetpack,Android Workmanager,我已经设置了一名工作经理,定期在特定时间执行任务。但它正在与其他android版本(如25或更低版本)合作。当我在API 28上运行它时,当我从最近的列表中清除应用程序时,它将停止触发广播 我在活动中安排了这样的工作经理: OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(MyWorker.class).setInitialDelay(15, TimeUnit.SECONDS).build(); WorkManager.ge

我已经设置了一名工作经理,定期在特定时间执行任务。但它正在与其他android版本(如25或更低版本)合作。当我在API 28上运行它时,当我从最近的列表中清除应用程序时,它将停止触发广播

我在活动中安排了这样的工作经理:

 OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(MyWorker.class).setInitialDelay(15, TimeUnit.SECONDS).build();
 WorkManager.getInstance().enqueue(mywork);
这是我的工人阶级:

public class MyWorker extends Worker {
    @NonNull
    @Override
    public Worker.WorkerResult doWork() {
        // Do the work here
        Mylogger.getInstance().printLog("MyWorr","doWork()");
        //fire broadcast from here
        Intent broadcastIntent = new Intent(getApplicationContext(), TimeMatchingBroadcastReceiver.class);
        getApplicationContext().sendBroadcast(broadcastIntent);
        CommonUtils.scheduleWorkManager();
        // Indicate success or failure with your return value:
        return WorkerResult.SUCCESS;

        // (Returning RETRY tells WorkManager to try this task again
        // later; FAILURE says not to try again.)
    }
}
我想在所有版本的android上关闭或清除最近列表中的应用程序时执行工作管理器。 谢谢。

如果您浏览,您会发现:

不幸的是,一些设备将从“最近”菜单中终止应用程序作为强制停止。股票安卓并没有做到这一点。当应用程序被强制停止时,它无法执行作业、接收警报或广播等。因此,不幸的是,我们无法解决它-问题在于操作系统,没有解决方法

这是一个众所周知的问题。为了节省电池,许多制造商强制关闭应用程序,从而取消所有周期任务、警报和广播接收器等。主要制造商有OnePlus(您可以选择toogle)、Redmi、Vivo、Oppo、Huwaei

每个设备都有AutoStart Manager/AutoLaunch/StartManager类型的优化管理器。防止后台活动再次启动。您必须手动要求用户将您的应用程序列入白名单,这样应用程序才能自动启动其后台进程。关注并链接,了解更多信息

中给出了为不同制造商添加白名单的方法。即使在添加到白名单后,您的应用程序也可能因为瞌睡模式而无法工作,因为您必须

此外,如果您可能想知道,像Gmail/Hangout/WhatsApp/Slack/LinkedIn等应用程序已经被这些AutoStart经理列入了白名单。因此,对它们的背景过程没有影响。您始终会收到及时的更新和通知


附言:从我自己的答案中抄袭而来

根据Ankuraurag2的答案,确实有一些安卓品牌在最近的一次杀戮后正在强制停止应用程序。 在work manager google问题跟踪器中检查此问题 对于标签#2

即使是我,在杀死了最近的设备(华为BKL-L09)上的应用程序后,也曾试图在后台运行此项工作,但未能做到这一点

在将自动启动选项更改为手动后,已选中该选项,则该选项工作正常。 但是对于用户体验来说,告诉用户更改自动启动选项并不是一个好主意,我们无法确定它是开还是关

搜索后,我给出了一个补丁,我认为这不是一个更好的解决方案,但可以检查它是否为我工作

首先,创建一个服务,用于标识终止应用程序进程 我引用了这个url

UserKillingService

class UserKillingService : Service() {

    override fun onBind(intent: Intent?)= null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int)= Service.START_NOT_STICKY

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onTaskRemoved(rootIntent: Intent?) {
        val i = Intent(this,StartUpActivity::class.java)
        i!!.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        i!!.putExtra("USER_KILLED", "USER_KILLED")
        startActivity(i)
        //stop the service
        stopSelf()
    }
}
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
        startService(Intent(baseContext,UserKillingService::class.java))
在清单中配置服务

<service android:name=".UserKillingService"
            android:stopWithTask="false"/>
此启动活动将在不强制停止应用程序的情况下完成。 此活动在未从UserKillingService触发时调用splashActivity

内部SplashActivity onCreate调用UserKillingService

class UserKillingService : Service() {

    override fun onBind(intent: Intent?)= null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int)= Service.START_NOT_STICKY

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onTaskRemoved(rootIntent: Intent?) {
        val i = Intent(this,StartUpActivity::class.java)
        i!!.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        i!!.putExtra("USER_KILLED", "USER_KILLED")
        startActivity(i)
        //stop the service
        stopSelf()
    }
}
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
        startService(Intent(baseContext,UserKillingService::class.java))

如果你有更好的解决办法,请告诉我。很抱歉,您的
返回WorkerResult.SUCCESS
告诉了我英语

我想您没有使用最新版本。尝试更新到最新版本(目前我认为是beta05')。@Ridcully感谢您的建议。让我更新并测试它。@Ridcully上一个问题通过增加工作库的版本得到了解决<代码>但现在它在小米5上不起作用。API级别28(8.0.0)当我从最近的列表中清除应用程序时。你能帮我一下吗?我想最好是你给WorkManager提交一个bug。根据各种beta版本的发布说明,他们似乎修复了许多类似于各种Android版本的bug。在Android 8(Oreo)上进行了测试。完美无瑕:砰的一声:。但我有一个问题。为什么在启动服务之前要检查“SDK\u INT>=VERSION\u CODES.O”@MarkKowalski,因为限制电池优化的设备高于牛轧糖。您可以查看有关android workmanager的视频。他们正试图通过联系制造商来解决这个问题。在安卓10(R)上测试。效果很好!真聪明!非常感谢。