1、Android 8.0 啓動 出錯 Not allowed to start service Intent { 包名} app is in background uid UidRecordhtml
錯誤緣由:Android 8.0 再也不容許後臺service直接經過startService方式去啓動。java
(application的onCreate方法中執行了一些初始化操做,而這個時候系統還認爲程序處於後臺,因此報出java.lang.IllegalStateException錯誤)android
解決思路:既而後臺服務會報錯,就變成前臺操做。bash
經過Service.startForeground()方法傳入notification變成前臺服務。可是這種方法不是想要的,本來的後臺服務變成了前臺服務,考慮能夠採用另外一個後臺執行任務的方法。這裏官方推薦使用JobScheduler。網絡
2、Android P系統限制了明文流量的網絡請求,okhttp3拋出異常解決;app
解決方法:ui
1.最好採用https請求;spa
2.項目的targetSdkVersion改成27如下;code
3.在res的xml目錄下,新建一個xml文件(名稱自定義,如:network_security_config.xml),內容以下:xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>複製代碼
在manifest清單文件配置application:
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...
/>複製代碼