티스토리 뷰
728x90
Android target SDK 를 Android O(API Level 26) 이상으로 하고 startService 를 호출하면 아래와 같은 exception 이 발생한다.
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent
Android O 부터 백그라운드 실행 제한이 적용되었기 때문이다.
Background vs Foreground Service
- Background service
- 일반적인 형태의 서비스로 메모리 자원이 부족할 경우 Low Memory Killer 에 의해 강제 종료 될 수 있음.
- onStartCommand() 에서 서비스 종료 시 동작 정의
- START_NOT_STICKY : 서비스 재 실행 안함
- START_STICKY : 서비스를 재 실행 함. null intent 의 onStartCommand()를 호출함
- START_REDELIVER_INTENT : 서비스를 재 실행하며, 기존의 Intent 파라메터를 이용하여 onStartCommand()를 호출함
- Foreground service
- 서비스의 우선 순위가 높아서, 시스템의 메모리 자원이 부족하더라도 강제로 종료시키지 않는다.
- 상태바에 Notification이 표시.
서비스는 기본적으로 background service 이기 때문에 foreground service를 만들기 위해서는 아래와 같은 방법이 필요하다.
- Android O 에서 새로 생긴 "Context.startForegroundService" 를 사용해 포그라운드 서비스를 시작할 수 있다. 앱이 백그라운드 상태일 때에도 이 메소드를 호출할 수 있지만, 서비스 생성 후 5초 이내에 생성된 서비스 의 "startForeground()" 를 호출해야 한다.
요렇게 수정하자
[Service 호출]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
context.startForegroundService(new Intent(context, TestService.class)); | |
} else { | |
context.startService(new Intent(context, TestService.class)); | |
} |
[TestService.java]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestService extends Service | |
{ | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
final String strId = getString(R.string.noti_channel_id); | |
final String strTitle = getString(R.string.app_name); | |
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
NotificationChannel channel = notificationManager.getNotificationChannel(strId); | |
if (channel == null) { | |
channel = new NotificationChannel(strId, strTitle, NotificationManager.IMPORTANCE_HIGH); | |
notificationManager.createNotificationChannel(channel); | |
} | |
Notification notification = new NotificationCompat.Builder(this, strId).build(); | |
startForeground(1, notification); | |
} | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
stopForeground(true); | |
} | |
} | |
} |
728x90
'Android > Service' 카테고리의 다른 글
Service (4) - bindService (0) | 2018.10.16 |
---|---|
Service (3) - bound service (0) | 2018.10.10 |
Service (2) - started service (3) | 2018.08.28 |
Service (1) (1) | 2018.08.21 |
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- onContextItemSelected
- onCreateContextMenu
- 리워드앱
- StringUtils
- 앱테크 추천
- android activity flag
- registerForContextMenu
- bindservice
- p2e
- WEMIX
- mPANDO
- M2E
- 안드로이드 인텐트
- StartService
- BroadcastReceiver
- 채굴앱
- Android Service
- android task
- RoomDatabase
- 앱테크
- notifyDataSetChanged
- android flag activity
- 무료채굴
- android intent
- 무료 채굴
- 리워드 어플
- task 생성
- Intent
- task
- 안드로이드 서비스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함