Oreo 的后台执行限制
Background Execution Limits on Oreo
大家好Android极客们,
我正在开发针对 Android 奥利奥 (O) 的 Android 应用程序。
minSdkVersion 21
targetSdkVersion 27
我知道 运行ning 后台服务有限制,我可以使用 startForeground(...)
服务方法来克服这些限制。
我的疑问是;
我应该对所有 android 版本使用这种 startForeground(...)
方法吗?由于调用此方法会显示一条通知,因此我想尽可能避免在 Oreo 之前的版本中使用它。
那么,为了避免在 Oreo 之前的设备上显示通知,我可以使用下面的代码片段吗?它会在 pre 和 post Oreo 的后台运行吗?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O /* 26 */) {
// Make the service as foreground service by calling startForeground method
} else {
// Nothing to do
}
总结一下我的问题:
targetSdkVersion 27
和 minSdkVersion 21
MyService
在 pre Oreo 和 post Oreo 中都应该是 运行 OS 个版本
- 仅postOreoOS版本
可以限制startForeground(...)
的使用吗
MyService should run in the background in both pre Oreo and post Oreo OS versions. Is it okay to limit the usage of startForeground(...) for only post Oreo OS version?
是的,可以这样用。
Service
将 运行 在 pre-oreo
设备的后台运行,但如果用户终止您的应用程序,Service
也会终止,除非您使用 startForeground
pre-oreo 也是如此。
WorkManager 适用于需要保证系统会 运行 它们的任务,即使应用程序退出也是如此
当您的用例还需要通知时,启动前景看起来很好:备份、导航、位置更新。这样的操作启动前景非常适合。
但也有其他情况不需要显示通知。
另外,当任务可延迟时,应始终使用它。
WorkManager uses an underlying job dispatching service when available
based on the following criteria:
Uses JobScheduler for API 23+ Uses a custom AlarmManager +
BroadcastReceiver implementation for API 14-22
link 更多
https://developer.android.com/reference/androidx/work/WorkManager?hl=en
大家好Android极客们,
我正在开发针对 Android 奥利奥 (O) 的 Android 应用程序。
minSdkVersion 21
targetSdkVersion 27
我知道 运行ning 后台服务有限制,我可以使用 startForeground(...)
服务方法来克服这些限制。
我的疑问是;
我应该对所有 android 版本使用这种 startForeground(...)
方法吗?由于调用此方法会显示一条通知,因此我想尽可能避免在 Oreo 之前的版本中使用它。
那么,为了避免在 Oreo 之前的设备上显示通知,我可以使用下面的代码片段吗?它会在 pre 和 post Oreo 的后台运行吗?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O /* 26 */) {
// Make the service as foreground service by calling startForeground method
} else {
// Nothing to do
}
总结一下我的问题:
targetSdkVersion 27
和minSdkVersion 21
MyService
在 pre Oreo 和 post Oreo 中都应该是 运行 OS 个版本- 仅postOreoOS版本 可以限制
startForeground(...)
的使用吗
MyService should run in the background in both pre Oreo and post Oreo OS versions. Is it okay to limit the usage of startForeground(...) for only post Oreo OS version?
是的,可以这样用。
Service
将 运行 在 pre-oreo
设备的后台运行,但如果用户终止您的应用程序,Service
也会终止,除非您使用 startForeground
pre-oreo 也是如此。
WorkManager 适用于需要保证系统会 运行 它们的任务,即使应用程序退出也是如此 当您的用例还需要通知时,启动前景看起来很好:备份、导航、位置更新。这样的操作启动前景非常适合。 但也有其他情况不需要显示通知。 另外,当任务可延迟时,应始终使用它。
WorkManager uses an underlying job dispatching service when available based on the following criteria: Uses JobScheduler for API 23+ Uses a custom AlarmManager + BroadcastReceiver implementation for API 14-22
link 更多 https://developer.android.com/reference/androidx/work/WorkManager?hl=en