본문 바로가기
개발 과정/문제 해결

java.lang.SecurityException: Permission Denial: startForeground 오류 해결하기

by 개발자D 2024. 11. 27.

java.lang.SecurityException: Permission Denial: startForeground 오류 해결하기

Logcat

java.lang.SecurityException: Permission Denial: startForeground

 

Foreground Service를 시작할 때(startForeground) 발생하는 오류입니다.

 

🟡 오류 해결하기 🟡

우선, Manifest에 FOREGROUND_SERVICE 권한이 제대로 선언되어 있는지 확인합니다.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

 

- 안드로이드 9 (API 28) 버전부터는 Foregroud Service를 시작할 때 Notification을 제공해야 합니다.

- 안드로이드 10 (API 29) 버전부터는 백그라운드에서 Foreground Service를 시작할 수 없습니다.

 

서비스에서 위치 정보를 사용한다면 위치 정보 관련 권한을 런타임 시 요청해야 합니다.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    ActivityCompat.requestPermissions(
        this,
        arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.FOREGROUND_SERVICE),
        REQUEST_CODE
    )
}