1. 程式人生 > >Caused by java.lang.IllegalStateException Not allowed to start service Intent報錯問題分析

Caused by java.lang.IllegalStateException Not allowed to start service Intent報錯問題分析

Caused by: java.lang.IllegalStateException: Not allowed to start service Intent

Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=package/xxx.xxxService }: app is in background uid UidRecord

Android 8.0(+)不允許在後臺啟動/建立Service,直接呼叫context.startService(intent)會宕機。

臨時解決辦法:

將編譯版本和目標版本降級到25,就可以避免因為8.0(26)不允許APP在後臺啟動Service而宕機。因為系統版本向後相容。
根本解決辦法:
1、SDK26 啟動Service的方法:context.startForegroundService(context, Service.class);

2、SDK26 Service啟動時,即在onCreate方法中,context.startForeground(id, new Notification());