본문 바로가기

android

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

java.lang.IllegalStateException: Not allowed to start service Intent { act=com.sesc.extension.action.PARSER pkg=com.sesc.extension (has extras) }: app is in background uid null

<원인> 오류 안드로이드가 일정 버전 이상으로 올라갈시 intent 서비스에서 startService 함수를 사용할수 없다.

< 해결방안>

getContext().startService(intent); 부분을 getContext().startForegroundService(intent); 함수로 바꿔줘야함

startForegroundService()는 api level 26의 레벨을 요구하기 때문에 if 문 조건으로 레벨 이하에서는 기존의 코드를 사용하게 하면 된다.

if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.0){

getContext().startForegroundService(intent)

}else{

getContext().startService(intent);

}

반응형