2. This presentation is provided on a strictly private and confidential basis for information purposes only.
Daniel Deng
Android Developer
FB: Daniel Deng
LinkedIn: daniel-deng-97742454
3. This presentation is provided on a strictly private and confidential basis for information purposes only.
今天不會介紹 Android Oreo 的新功能
4. This presentation is provided on a strictly private and confidential basis for information purposes only.
也不會逐一說明所有的 API 變更
詳情請自行參閱 https://developer.android.com/about/versions/oreo/android-8.0-changes.html
5. This presentation is provided on a strictly private and confidential basis for information purposes only.
那今天要介紹什麼 ?
6. This presentation is provided on a strictly private and confidential basis for information purposes only.
我們實際將 targetSdkVersion 升至
Android 8.0 遇到的問題
7. This presentation is provided on a strictly private and confidential basis for information purposes only.
Outline
• 關於版本
• 背景執行限制
• Notification Channel
• Alert Windows
• GCM Service
• AudioAttributes
8. This presentation is provided on a strictly private and confidential basis for information purposes only.
關於版本 (1/5)
• minSdkVersion
• compileSdkVersion
• targetSdkVersion
9. This presentation is provided on a strictly private and confidential basis for information purposes only.
關於版本 (2/5)
• minSdkVersion
• 允許安裝的最低版本要求
• 會相依使用的 Library 中的 minSdkVersion
• 參考 Android OS 分佈統計決定
2017/11/9 Android OS 版本占比
10. This presentation is provided on a strictly private and confidential basis for information purposes only.
關於版本 (3/5)
• compileSdkVersion
• 使用哪個 Android SDK 版本編譯
• 編譯後可能會出現錯誤或警告
• 會出現低於此版本的棄用 API 警告
• 至少需和使用的 Support Library 同個版本
11. This presentation is provided on a strictly private and confidential basis for information purposes only.
關於版本 (4/5)
• targetSdkVersion
• 向下兼容至哪個版本
• 會套用該版本的行為變化
• 一定要事先測試
12. This presentation is provided on a strictly private and confidential basis for information purposes only.
關於版本 (5/5)
Q: Android 6.0 (API Level 23) 調整了權限授權行為,小明的 App 使
用了列為危險權限的「READ_CONTACTS」。
他的 App compileSdkVersion 為 23,targetSdkVersion 為 21
請問小明的 App 可以不做 runtime permission 的調整在 Android 6.0
的手機上執行嗎?
13. This presentation is provided on a strictly private and confidential basis for information purposes only.
關於版本 (5/5)
Q: Android 6.0 (API Level 23) 調整了權限授權行為,小明的 App 使
用了列為危險權限的「READ_CONTACTS」。
他的 App compileSdkVersion 為 23,targetSdkVersion 為 21
請問小明的 App 可以不做 runtime permission 的調整在 Android 6.0
的手機上執行嗎?
A: 可以,只要 targetSdkVersion 未升至行為變更的版本,就能繼續向下
相容
15. This presentation is provided on a strictly private and confidential basis for information purposes only.
背景執行限制
Android 8.0 為提高電池續航時間,當 App 進入背景且沒有執行中的
Service,系統會解除 App 持有的 wakelocks 並限制其操作。
• 背景服務限制
• 廣播限制
16. This presentation is provided on a strictly private and confidential basis for information purposes only.
背景服務限制 - 在背景時如何啟動服務? (1/2)
❖ Android 8.0 以前
➢ 可在背景啟動服務,依據需求再提高為前景服務
Context.startService(serviceIntent);
Service.startForeground(notificationId, notification);
17. This presentation is provided on a strictly private and confidential basis for information purposes only.
❖ Android 8.0 以前
➢ 可在背景啟動服務,依據需求再提高為前景服務
Context.startService(serviceIntent);
Service.startForeground(notificationId, notification);
❖ Android 8.0 以後
➢ 先透過新的 API 啟動前台服務,接著在 5 秒內設為前景服務
Context.startForegroundService()
Service.startForeground(notificationId, notification);
背景服務限制 - 在背景時如何啟動服務? (2/2)
18. This presentation is provided on a strictly private and confidential basis for information purposes only.
❖ Android 8.0 以前
➢ 可在背景啟動服務,依據需求再提高為前景服務
Context.startService(serviceIntent);
Service.startForeground(notificationId, notification);
❖ Android 8.0 以後
➢ 先透過新的 API 啟動前台服務,接著在 5 秒內設為前景服務
Context.startForegroundService()
Service.startForeground(notificationId, notification);
背景服務限制 - 在背景時如何啟動服務? (2/2)
若未即時設置會被
系統視為 ANR
19. This presentation is provided on a strictly private and confidential basis for information purposes only.
廣播限制 - 隱式廣播 (Implicit broadcasts) (1/5)
➢ Explicit Intents
○ 有指定明確目標的 Intent,註冊於 AndroidManifest.xml 中仍可正常接收
20. This presentation is provided on a strictly private and confidential basis for information purposes only.
➢ Explicit Intents
○ 有指定明確目標的 Intent,註冊於 AndroidManifest.xml 中仍可正常接收
➢ Implicit Intents
○ 沒有明確目標的 Intent
註冊在 AndroidManifest.xml 中的 Implicit Intent Receiver 會收不到 Intent
廣播限制 - 隱式廣播 (Implicit broadcasts) (2/5)
21. This presentation is provided on a strictly private and confidential basis for information purposes only.
● 例外的 Implicit Intent,仍可繼續在 AndroidManifest.xml 中監聽
○ ACTION_LOCKED_BOOT_COMPLETED, ACTION_BOOT_COMPLETED
○ ACTION_USER_INITIALIZE
○ ACTION_LOCALE_CHANGED
○ ACTION_USB_ACCESSORY_ATTACHED, ACTION_USB_ACCESSORY_DETACHED,
ACTION_USB_DEVICE_ATTACHED, ACTION_USB_DEVICE_DETACHED
○ ACTION_CONNECTION_STATE_CHANGED, ACTION_CONNECTION_STATE_CHANGED,
ACTION_ACL_CONNECTED, ACTION_ACL_DISCONNECTED
○ ACTION_CARRIER_CONFIG_CHANGED
○ LOGIN_ACCOUNTS_CHANGED_ACTION
○ ACTION_PACKAGE_DATA_CLEARED
○ ACTION_PACKAGE_FULLY_REMOVED
○ ACTION_NEW_OUTGOING_CALL
○ ACTION_DEVICE_OWNER_CHANGED
○ ACTION_EVENT_REMINDER
○ ACTION_MEDIA_MOUNTED, ACTION_MEDIA_CHECKING, ACTION_MEDIA_UNMOUNTED,
ACTION_MEDIA_EJECT, ACTION_MEDIA_UNMOUNTABLE, ACTION_MEDIA_REMOVED,
ACTION_MEDIA_BAD_REMOVAL
○ SMS_RECEIVED_ACTION, WAP_PUSH_RECEIVED_ACTION
廣播限制 - 隱式廣播 (Implicit broadcasts) (3/5)
22. This presentation is provided on a strictly private and confidential basis for information purposes only.
❖ 未在例外中的隱式廣播該如何監聽?
➢ Runtime 在 Java code 中註冊
廣播限制 - 隱式廣播 (Implicit broadcasts) (4/5)
23. This presentation is provided on a strictly private and confidential basis for information purposes only.
❖ 未在例外中的隱式廣播該如何監聽?
➢ Runtime 在 Java code 中註冊
➢ 或改用 JobScheduler
廣播限制 - 隱式廣播 (Implicit broadcasts) (5/5)
24. This presentation is provided on a strictly private and confidential basis for information purposes only.
背景執行限制 - JobIntentService (1/2)
用來執行加入至 JobScheduler 中的 Job 的輔助工具
25. This presentation is provided on a strictly private and confidential basis for information purposes only.
背景執行限制 - JobIntentService (2/2)
26. This presentation is provided on a strictly private and confidential basis for information purposes only.
背景執行限制 - JobIntentService (2/2)
Android O 以下被視為普通的 IntentService 使用 startService() 來執行
34. This presentation is provided on a strictly private and confidential basis for information purposes only.
Alert Windows (1/2)
使用 SYSTEM_ALERT_WINDOW 權限的 App 無法再使用以下類型參數來顯示在其他 App 之上
35. This presentation is provided on a strictly private and confidential basis for information purposes only.
Alert Windows (2/2)
須改用
另外有一些新的特性
• 浮動視窗始終顯示在狀態欄和輸入法下方
• 系統可移動浮動視窗或調整其大小
• 用戶可透過系統狀態欄關閉浮動視窗
37. This presentation is provided on a strictly private and confidential basis for information purposes only.
GCM Service (1/3)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent {
act=com.google.android.c2dm.intent.RECEIVE app is in background}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1505)
at android.app.ContextImpl.startService(ContextImpl.java:1461)
at android.content.ContextWrapper.startService(ContextWrapper.java:644)
at android.content.ContextWrapper.startService(ContextWrapper.java:644)
at
android.support.v4.content.WakefulBroadcastReceiver.startWakefulService(WakefulBroadcastReceiver.java:99)
at com.google.android.gms.gcm.GcmReceiver.doStartService(GcmReceiver.java:22)
at com.google.android.gms.gcm.GcmReceiver.zzd(GcmReceiver.java:0)
at com.google.android.gms.gcm.GcmReceiver.onReceive(GcmReceiver.java:98)
當 App 未啟動時收到推播..
38. This presentation is provided on a strictly private and confidential basis for information purposes only.
GCM Service (2/3)
AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start receiver com.google.firebase.iid.FirebaseInstanceIdReceiver:
java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.firebase.MESSAGING_EVENT
app is in background }
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3259)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
升ㄔ
轉移至 FCM (Firebase Cloud Messaging) 後..
39. This presentation is provided on a strictly private and confidential basis for information purposes only.
GCM Service (3/3)
升ㄔ
關注 Firebase Android SDK 釋出的修正!
● Release notes: https://firebase.google.com/support/release-notes/android
● Reference:
https://stackoverflow.com/questions/46117554/firebasemessagingservice-crashes-on-android-o-d
ue-to-background-execution-limits#comment81612160_46117554
41. This presentation is provided on a strictly private and confidential basis for information purposes only.
AudioAttributes (1/2)
所有音頻相關的 API 都應改用 AudioAttributes 來描述 use case
原來的寫法:
42. This presentation is provided on a strictly private and confidential basis for information purposes only.
AudioAttributes (2/2)
所有音頻相關的 API 都應改用 AudioAttributes 來描述 use case
新的寫法:
43. This presentation is provided on a strictly private and confidential basis for information purposes only.
References
● https://developer.android.com/about/versions/oreo/android-8.0-changes.html
● https://developer.android.com/about/versions/oreo/android-8.0-migration.html
● https://developer.android.com/about/versions/oreo/background.html
● https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo-ab384762a66c
● https://developers.google.com/cloud-messaging/android/android-migrate-fcm