728x90

안드로이드 8.0 에서 서비스를 실행할 때 아래코드를 추가해줘야 에러가 발생하지 않는다.


// 서비스를 실행할 때
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(new Intent(context, ServedService.class));
} else {
    context.startService(new Intent(context, ServedService.class));
}

// 서비스 파일 내에서
@Override
public void onCreate() {
    super.onCreate();
    startForeground(1,new Notification()); // 추가
}

@Override
public void onDestroy() {
    // 서비스가 종료될 때 실행
    super.onDestroy();
    stopForeground(true);
}



블로그 이미지

Link2Me

,