SlideShare a Scribd company logo
Background processing after Android O
ft. WorkManager
Carter Chen @ 第 44 回 Android Developer 讀書會
Oct 20, 2018
Carter Chen
https://github.com/KazafChen
https://medium.com/@kazafchen
154
為什麼來讀書會分享?
Background processing after android O ft. WorkManager
取之於讀書會,用之於讀書會
平時都在讀書會抱大腿+潛水⋯⋯
Background processing after android O ft. WorkManager
Content
● Confronting challenges
● Why WorkManager
● Conclusion
Content
● Confronting challenges
● Why WorkManager
● Conclusion
Medium持續補完中:tinyurl.com/carter1020
Background processing after android O ft. WorkManager
Background processing after android O ft. WorkManager
Background processing after android O ft. WorkManager
設定背景任務或定時任務
● start 一個 Service,然後
Oreo以前,你可能這樣做過
targetSdkVersion 25
如果給Service的壽命一個期限
如果給Service的壽命一個期限
那會是⋯⋯一萬年
targetSdkVersion >= 26
Background processing after android O ft. WorkManager
“Foreground” in Android O
● Visible Activity
● 是Foreground Service
● 跟IME、Wallpaper service、Notification listener、Voice 或
text servicer進行綁定
那使用Foreground Service不就好啦
startForeground()綁架了Notification
Background processing after android O ft. WorkManager
Background processing after android O ft. WorkManager
Background processing after android O ft. WorkManager
Background processing after android O ft. WorkManager
Service
System
How about JobScheduler?
JobScheduler的用途與地位?
● There are several APIs that your app can use to schedule background
work. Chief among these options is JobScheduler
● In most cases, apps can work around these limitations by using
JobScheduler jobs.
● Android 8.0 offers several improvements to JobScheduler that make it
easier to replace services and broadcast receivers with scheduled
jobs
Schedule jobs intelligently | Android Developers
精美的API
使用起來真簡單
Everything looks fine, but...
Background processing after android O ft. WorkManager
Issues exist in API 21, 22
Services. The life with/without. And WorkManager. – Google Developers
Experts – Medium
如果想要向下相容?
Background processing after android O ft. WorkManager
WorkManager
WorkManager能幹嘛?
● Support for both asynchronous one-off and periodic tasks
● Support for constraints such as network conditions, storage space, and
charging status
● Chaining of complex work requests, including running work in parallel
● Output from one work request used as input for the next
● Handles API level compatibility back to API level 14
● Works with or without Google Play services
● Follows system health best practices
● LiveData support to easily display work request state in UI
Background processing after android O ft. WorkManager
長話短說⋯⋯
1. 我可以向下相容到API 14
2. JobScheduler會的,我都會
3. 高難度的姿勢難不倒我
4. 可以用LiveData觀察任務進行的狀態
長話短說⋯⋯
1. 我可以向下相容到API 14
2. JobScheduler會的,我都會
3. 高難度的姿勢招式難不倒我(例如Chaining)
4. 可以用LiveData觀察任務進行的狀態
Android Jetpack: easy background processing with WorkManager (Google I/O ‘18)
1. 相容到API 14的秘密
Android Jetpack: easy background processing with WorkManager (Google I/O ‘18)
插播:What is JobDispatcher?
JobDispatcher:Concerns
● Depend on Google Play Service
● Latest version: 0.8.5
WorkManager將根據版本自動選擇API
2. 別人會的,我都會?
● 簡單的 Job / Task service實作( Job 在 WorkManager 中為 Work )
● 一次性的Work
● 週期性的Work
● 啟動條件設置(Trigger constraint)
● 重試策略設置(Retry strategy)
● 參數傳遞
簡單的 Job / Task service實作
一次性的Work
週期性的Work
啟動條件設置
重試策略設置
參數傳遞
參數傳遞:接收參數
參數傳遞:可以傳什麼樣的資料?
Work Chaining
這個我就不知道別人會不會了
先來看看單發的例子
Chaining:連發
Chaining + 傳遞參數
Chaining:1st work has input
Chaining:1st worker has input & output
Chaining:1st worker’s output = 2nd worker’s input
Chaining:1st worker’s output = 2nd worker’s input
● 我跳進來啦,又跳出去啦
Chaining:隨心所欲,進進出出
Parallel:齊發
WTF?
What The Fantastic chaining~~~
What The Fantastic chaining~~~
What The Fantastic chaining~~~
Combine multiple chainings~~~
耶,串完Chaining辣
Work開始依序執行辣
咦!
越想越不對勁~~~
● ExistingWorkPolicy:REPLACE / KEEP / APPEND
beginUniqueWork:新chain如何處置舊chain?
說了這麼多⋯⋯阿Chaining的結果勒?
說了這麼多⋯⋯阿「結果」勒?
● WorkManager 中 Work 的「處理狀態」被封裝為 WorkStatus
● 等 WorkStatus.getState().isFininshed() 的時候再取值
How to get WorkStatus?
把 LiveData<WorkStatus> observe起來
States of WorkStatus
● ENQUEUED
● RUNNING
● SUCCEEDED
● FAILED
● BLOCKED
● CANCELLED
Work Cancellation
Work Cancellation
來做一個下結論的動作
WorkManager⋯⋯
● 在需要保證任務執行的同時,達成了省電的效果
● API 它全都要(至少到 API 14)
● 人性化的設計,讓開發者可以容易地去組織任務流程
● Condition-based,not time-based,所以想在精確的時間點上執行任務
可能得自己另外刻AlarmManager,或依賴3rd library(如android-job)
● 目前還在alpha,本文使用版本為1.0.0-alpha09
為什麼知道這些?
● 科技始終來自於惰性
為什麼知道這些?
● 科技始終來自於惰性人性
為什麼知道這些?
● 科技始終來自於惰性人性
● 不想自己重新造輪子
為什麼知道這些?
● 科技始終來自於惰性人性
● 不想自己重新造輪子
● 有這方面的
為什麼知道這些?
● 科技始終來自於惰性人性
● 不想自己重新造輪子
● 有這方面的 需求
參考資料
● Youtube:Android Developers
○ Android Jetpack: easy background processing with WorkManager (Google I/O '18)
● Codelab
○ Background Work with WorkManager
● Medium
○ Scheduling jobs like a pro with JobScheduler
○ Android Scheduling Background Services( A developer’s nightmare)
○ Exploring Jetpack: The power of chains in the WorkManager APIs
○ Let’s Work manager do all the Background processing
Q & A

More Related Content

PPTX
极速 Angular 开发:效能调校技巧 (ngChina 2019)
PDF
超酷炫科幻 UI:QML 入門
PPTX
全新的Qt5
PPT
Grails敏捷项目开发
PDF
用最潮的 Java script 盡情開發 kde qt 程式
PDF
嵌入式測試驅動開發
PDF
Non-MVC Web Framework
PDF
Unit testing
极速 Angular 开发:效能调校技巧 (ngChina 2019)
超酷炫科幻 UI:QML 入門
全新的Qt5
Grails敏捷项目开发
用最潮的 Java script 盡情開發 kde qt 程式
嵌入式測試驅動開發
Non-MVC Web Framework
Unit testing
Ad

Background processing after android O ft. WorkManager