解決Android8.0以後開啓service時報錯IllegalStateException: Not allowed to start service Intent ...android
背景:
項目測試時發現的,在雙擊返回鍵關閉應用後(並未殺死後臺)從新打開APP,其餘手機都OK,可是8.0的手機會出現較頻繁的crash。檢查代碼,問題鎖定在從新開啓應用時的startService()上。web
查找資料說是Android 8.0 再也不容許後臺service直接經過startService方式去啓動,不然就會引發IllegalStateException微信
緣由
Android 8.0 有一項複雜功能;系統不容許後臺應用建立後臺服務。 所以,Android 8.0 引入了一種全新的方法,即 Context.startForegroundService(),以在前臺啓動新服務。 在系統建立服務後,應用有5秒的時間來調用該服務的 startForeground() 方法以顯示新服務的用戶可見通知。若是應用在此時間限制內未調用 startForeground(),則系統將中止服務並聲明此應用爲 ANR。app
遇到的問題
可是目前在調用:context.startForegroundService(intent)時報以下ANR,startForegroundService()文檔說明在service啓動後要調用startForeground()。ide
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
完整解決步驟:測試
1. 添加權限
<!--android 9.0上使用前臺服務,須要添加權限,此權限爲級別爲nomarl-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
2. 啓動server(引用啓動5秒內要啓動server)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, MyService.class));
} else {
context.startService(new Intent(context, MyService.class));
}
而後必須在Myservice中調用startForeground():ui
3. Server中onCreate方法中調用startForeground()
public static final String CHANNEL_ID_STRING = "service_01";
private Notification notification;
@Override
public void onCreate() {
super.onCreate();
//適配8.0service
NotificationManager notificationManager = (NotificationManager) MyApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(CHANNEL_ID_STRING, getString(R.string.app_name),
NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
startForeground(1, notification);
}
}
4. 在onStart裏再次調用startForeground()
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
startForeground(1, notification);
}
}
註解:
Android 8.0 系統不容許後臺應用建立後臺服務,故只能使用Context.startForegroundService()啓動服務spa
建立服務後,應用必須在5秒內調用該服務的 startForeground() 顯示一條可見通知,聲明有服務在掛着,否則系統會中止服務 + ANR 套餐送上。.net
Notification 要加 Channel,系統的要求code
爲何要在onStart裏再次調用startForeground()?答:這一條主要是針對後臺保活的服務,若是在服務A運行期間,保活機制又startForegroundService啓動了一次服務A,那麼這樣不會調用服務A的onCreate方法,只會調用onStart方法。若是不在onStart方法裏再掛個通知的話,系統會認爲你使用了 startForegroundService 卻不在 5 秒內給通知,很傻地就中止服務 + ANR 套餐送上了。
本文分享自微信公衆號 - 喘口仙氣(gh_db8538619cdd)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。