简体   繁体   English

onTaskRemoved()在Android 8.0 Oreo API级别26上不起作用

[英]onTaskRemoved() doesn't work on Android 8.0 Oreo API level 26

On device with Android Oreo 8.0 (API level 26) JobService.onTaskRemoved() isn't called 在装有Android Oreo 8.0(API级别26)的JobService.onTaskRemoved()上,未调用JobService.onTaskRemoved()

  • How to listen for such an event? 如何收听这样的事件?

Code used which stopped working: 使用的代码停止工作:

class TaskRemovalWatcherService: JobIntentService() {
    ...
    override fun onTaskRemoved(rootIntent: Intent?) {  
    //not called on Android Oreo

        super.onTaskRemoved(rootIntent);   
        onAppRemovedFromRecentApps()
    }
}

When App-Task is removed from recent apps I need to 从最近的应用程序中删除应用程序任务时,我需要

  1. Stop a process 停止进程
  2. Finish 2nd AppTask 完成第二个AppTask

if AppTasks.size = 2 and 1 task is removed from recent apps, then process is NOT killed, because 2nd AppTask still in memory. 如果AppTasks.size = 2并且从最近的应用程序中删除了1个任务,则不会终止进程,因为第2个AppTask仍在内存中。

Update: 更新:

  1. when use JobIntentService.enqueueWork then right after JobIntentService.onHandleWork() JobIntentService.onDestroy() is called 当使用JobIntentService.enqueueWork则在JobIntentService.onHandleWork() JobIntentService.onDestroy()
  2. when use JobService.schedule() throws IllegalArgumentException when schadule job without constraints. 当使用JobService.schedule()作业无约束时,使用JobService.schedule()会抛出IllegalArgumentException

PS The idea of checking each X seconds isTaskWasRemoved seems not a good one. PS 每隔X秒检查一次的想法isTaskWasRemoved似乎不是一个好主意。

您可以覆盖JobService#onStop() ,然后创建一条消息并将其传递给其他服务,当该服务收到该消息时,您可以清理它的工作并杀死它。

The expected behavior of START_STICKY might have changed due to custom implementation by device manufacturer or due to new implementation in android Oreo START_STICKY的预期行为可能由于设备制造商的自定义实现或android Oreo中的新实现而发生了变化

You can use JobScheduler to monitor a service lifecycle after regular interval. 您可以使用JobScheduler定期监控服务生命周期。 If service is destroyed, then you can perform your task in onDestroy() method just before service is actually being destroyed. 如果服务被销毁,则可以在服务真正被销毁之前使用onDestroy()方法执行任务。

This will remove your dependency from onTaskremoved() method. 这将从onTaskremoved()方法中删除您的依赖onTaskremoved()

Please refer below question with similar problem onTaskRemoved() not getting called when Home press & kill the app 请按以下问题,并在按下Home键并杀死应用程序时未调用onTaskRemoved()类似问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Xamarin.Forms中使用Android支持库API Level 26(MonoAndroid 8.0 Oreo)? - Using android support libraries API Level 26 (MonoAndroid 8.0 Oreo) in Xamarin.Forms? Azure NotificationHub 推送通知不适用于 Android 8.0 API 26 - Azure NotificationHub push notification doesn't work for Android 8.0 API 26 找不到 Android 8.0-8.1 (Oreo) API 26-27 ARM 系统映像 - Can't find Android 8.0-8.1 (Oreo) API 26-27 ARM System Images 从API 26:Android 8.0 Oreo开始,不推荐使用Build.SERIAL - Build.SERIAL is deprecated as of API 26: Android 8.0 Oreo 如何在 Android 8.0 中正确更新小部件 - Oreo - API 26 - How To Properly Update A Widget In Android 8.0 - Oreo - API 26 在Android 8.0 Oreo上未出现NOtification - NOtification doesn't appear on Android 8.0 Oreo 无法在 android oreo(API 级别 26)上以设备管理员身份激活应用 - Unable to activate app as device admin on android oreo(API level 26) Service Syncronize不适用于API => 26级 - Service Syncronize doesn't work on API => level 26 如何在Android 8.0 Oreo API 26及更高版本上检测到应用终止 - How can I detect app kill on Android 8.0 Oreo API 26 and after 在Android 8.0(API 26,Oreo)上Twilio聊天客户端关闭期间崩溃 - Crash during Twilio chat client shutdown on Android 8.0 (API 26, Oreo)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM