Android 11: IntentService is deprecated

发布时间: 2021-02-28 16:52:12 作者: 大象笔记

使用 IntentService 实现了一个耗时的功能,但是 Android Studio 编译时警告:

IntentService is deprecated

官方文档

https://developer.android.google.cn/reference/android/app/IntentService

This class was deprecated in API level 30. IntentService is subject to all the background execution limits imposed with Android 8.0 (API level 26). Consider using WorkManager or JobIntentService, which uses jobs instead of services when running on Android 8.0 or higher.

API Level 30 对应的 Android 版本

也就是说 Android 11 之后,IntentService 将被废弃。

IntentService 被废弃了,那前台 Service 呢?

https://developer.android.com/guide/components/services

Note: The WorkManager API offers a flexible way of scheduling tasks, and is able to run these jobs as foreground services if needed. In many cases, using WorkManager is preferable to using foreground services directly.

许多场景下,即便是前台 service 也被推荐使用 WorkManager 替代。

尴尬的 WorkManager

看了一下 WorkManager,使用起来也非常简单。 但是 《第一行代码 - Android 第三版》中说,很多国产的魔改 Android 系统并不支持 WorkManager 。。。

因为国产 Android 手机允许一键杀死所有不在白名单中的 App,而被杀死的 App 无法运行 WorkManager 的后台任务。

所以,核心功能还是不推荐放在 WorkManager 中。要么提示用户添加应用白名单。

结论

我还是试试 foreground service,或者 JobIntentService 吧。

我是一名山东烟台的开发者,联系作者