SlideShare une entreprise Scribd logo
1  sur  15
Service.
Service란?
A Service is an application component that can perform long-
running operations in the background and does not provide a
user interface. Another application component can start a service
and it will continue to run in the background even if the user
switches to another application.
활용 예.
• handle network transactions
• play music
• perform file I/O
• interact with a content provider
• 기타 등등
배경에서 진행.
Service의 2가지 형태.
Type 1.
호출이 되고, 그
뒤에 주어진 일을
백그라운드에서
수행하는 데몬 같
은 형태.
Type 2.
메소드를 호출시
키고, 이를 다른
곳에 호출을 시켜
수행할 수 있는
원격 호출 인터페
이스 형태.
2가지 특성 모두 가질 수 있음.
먼저 Deamon 형태 부터~
Service에서 Thread?
Service는 메인 thread에서 실행이 되고 백그라운드에서 사
용자가 알수 없도록 실행되어야 하기 때문에 별도의 thread를 통해서
MediaPlayer객체를 실행합니다. 그렇지 않으면 MediaPlayer가 실
행하는 동안에 사용자가 화면상에서의 액션을 취하려면
delay가 발생할 것입니다.
간단하게 다시 봅시다.
별도 Thread가 아니라서,
본 Thread가 계산에 바빠서 추가적으로
화면에서 다른 화면을 눌러도 응답이 없습니다.
쓰레드로 분리하고 Stop 시키기.
stopService
Service는 별도 Thread가 아니다.
A service runs in the main thread of its hosting process—the service
does not create its own thread and does not run in a
separate process (unless you specify otherwise). This means that, if
your service is going to do any CPU intensive work or blocking operations
(such as MP3 playback or networking), you should create a new
thread within the service to do that work. By using a
separate thread, you will reduce the risk of Application Not Responding
(ANR) errors and the application's main thread can remain
dedicated to user interaction with your activities.
서비스는 정지 안 시키면 ..
서비스는 명시적으로 정지를 시키거나 (stopService) 아니면 자신의
동작을 마치면 멈추게 됩니다.
그렇다면 이 경우를 생각합니다.
Service가 내부적으로 Thread를 만들어서 5분에 한 번씩 메시지를 표
시하게 구현되어 있고, 이 Service가 start 된 상황입니다.
위 경우에는 App을 닫고 Home 화면으로 이동하거나 다른 App을 실
행 하더라도 지속적으로 메시지는 꾸준히 찍게 됩니다. Activity와 관
계 없이 별도 구동하기 때문입니다.
이러한 점을 명심해서, Service 생명주기를 꼬옥 유념하기 바랍니다.
서비스 return 값
START_STICTY
메모리부족이나 기타 상황에서 시스템이 강제로 service를 종료된 후 service가 재시작될 때
null Intent가 담긴 onStartCommand() 콜백 함수가 실행된다. 이 경우 null Intent로 호출 때의
경우를 처리해줘야 합니다.
예) 음악 재생 프로그램 경우, 백그라운드 작업이 계속되는 경우.
START_NOT_STICTY
이 경우는 프로세스가 강제로 종료되었을 경우 재 시작하지 않고 종료된 상태로 남게 됩니다. 특
정 동작이나 명령을 처리하기 위해 시작되는 서비스에 쓰입니다. 이런 서비스들은 보통 해당 명
령을 처리한 뒤 stopSelf를 이용해 종료하는 경우가 대다수.
예) App 업데이트 서비스 경우에, 리소스 경합인 현재에 다시 또 시도하지 않고 후일을 기약.
START_REDELIVER_INTENT
이 경우에는 프로세스가 강제로 종료되었을 경우 Intent가 다시 전달되어 재시작합니다. 단, 여
러 차례 시도후 작업이 종료되지 않으면 service는 재시작 되지 않습니다. 반드시 실행되어야 하
는 service에 해당이 됩니다
Service 다중 호출
startService 연속 호출
테스트 결과
IntentService라는 것도 있어요.
Service라는 것이 UI가 없이 뒤에서 도는 것이라고 흔히 이야기를 하지만,
메세지를 적절히 받아서 매번 처리를 해 나가야 합니다.
이를 위해서, 메시지를 받을 때 마다 원하는 작업을 하기 위해서는
Thread에 메시지가 들어오는 것을 모니터링하고, 들어올 때 처리를 해야
하지요. (이게 android에서 많이 나오는 handler 라고 하는 부분과 동일
형태입니다.)
이러한 것을 별도로 구현을 하는 것은 번거러울 수 있습니다. 이를 단순하
게 모두 구현해 둔 것이 IntentService입니다.
메시지 (Intent) 받아서 처리하는 형태를 추상화. 구조는 다음 페이지 참고
IntentService Sample.
BroadCastReceiver 형태로 구현도 가능.
관련 참고: http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/
Receiver는 10 초내에 작업을 마쳐야 합니다. 그렇기에 위 내용 중에서 IntentService 대신
BroadcastReceiver를 사용이 가능한 작업은 단순 작업일 때에만 하도록해야 하는 점 꼬옥 주의 합시다
출처: http://arabiannight.tistory.com/247

Contenu connexe

Similaire à 12 service

1120 현오뱅
1120 현오뱅1120 현오뱅
1120 현오뱅ljh606
 
딜라이트룸에서 일한.ssul
딜라이트룸에서 일한.ssul딜라이트룸에서 일한.ssul
딜라이트룸에서 일한.ssulJungwon An
 
08 fragment 태블릿 대응
08 fragment   태블릿 대응08 fragment   태블릿 대응
08 fragment 태블릿 대응운용 최
 
Linux screen manual (KOR ver)
Linux screen manual (KOR ver)Linux screen manual (KOR ver)
Linux screen manual (KOR ver)Kevin Kim
 
iOS Human Interface Guidelines 한글 정리 문서입니다.
iOS Human Interface Guidelines 한글 정리 문서입니다.iOS Human Interface Guidelines 한글 정리 문서입니다.
iOS Human Interface Guidelines 한글 정리 문서입니다.Dong Jin Lee
 
14 broad castreceiver
14 broad castreceiver14 broad castreceiver
14 broad castreceiver운용 최
 
iOS Human Interface Guidlines #12_SYS4U
iOS Human Interface Guidlines #12_SYS4UiOS Human Interface Guidlines #12_SYS4U
iOS Human Interface Guidlines #12_SYS4Usys4u
 

Similaire à 12 service (7)

1120 현오뱅
1120 현오뱅1120 현오뱅
1120 현오뱅
 
딜라이트룸에서 일한.ssul
딜라이트룸에서 일한.ssul딜라이트룸에서 일한.ssul
딜라이트룸에서 일한.ssul
 
08 fragment 태블릿 대응
08 fragment   태블릿 대응08 fragment   태블릿 대응
08 fragment 태블릿 대응
 
Linux screen manual (KOR ver)
Linux screen manual (KOR ver)Linux screen manual (KOR ver)
Linux screen manual (KOR ver)
 
iOS Human Interface Guidelines 한글 정리 문서입니다.
iOS Human Interface Guidelines 한글 정리 문서입니다.iOS Human Interface Guidelines 한글 정리 문서입니다.
iOS Human Interface Guidelines 한글 정리 문서입니다.
 
14 broad castreceiver
14 broad castreceiver14 broad castreceiver
14 broad castreceiver
 
iOS Human Interface Guidlines #12_SYS4U
iOS Human Interface Guidlines #12_SYS4UiOS Human Interface Guidlines #12_SYS4U
iOS Human Interface Guidlines #12_SYS4U
 

Plus de 운용 최

Wot(daliworks) 20131113
Wot(daliworks) 20131113Wot(daliworks) 20131113
Wot(daliworks) 20131113운용 최
 
NIPA SW Insight Report '13.08
NIPA SW Insight Report '13.08NIPA SW Insight Report '13.08
NIPA SW Insight Report '13.08운용 최
 
Agile Spirit Base On The Book "Agile Samuari"
Agile Spirit Base On The Book "Agile Samuari"Agile Spirit Base On The Book "Agile Samuari"
Agile Spirit Base On The Book "Agile Samuari"운용 최
 
21 application and_network_status
21 application and_network_status21 application and_network_status
21 application and_network_status운용 최
 
20 handler and_async_task
20 handler and_async_task20 handler and_async_task
20 handler and_async_task운용 최
 
18 안드로이드 리스트뷰_속도향상
18 안드로이드 리스트뷰_속도향상18 안드로이드 리스트뷰_속도향상
18 안드로이드 리스트뷰_속도향상운용 최
 
19 나만의 view 만들기
19 나만의 view 만들기19 나만의 view 만들기
19 나만의 view 만들기운용 최
 
17 adapter view & db
17 adapter view & db17 adapter view & db
17 adapter view & db운용 최
 
16 데이터 저장과 사용
16 데이터 저장과 사용16 데이터 저장과 사용
16 데이터 저장과 사용운용 최
 
15 content provider
15 content provider15 content provider
15 content provider운용 최
 
13 service 좀더
13 service 좀더13 service 좀더
13 service 좀더운용 최
 
11 tablet 대응 가이드
11 tablet 대응 가이드11 tablet 대응 가이드
11 tablet 대응 가이드운용 최
 
10 view orientation_change
10 view orientation_change10 view orientation_change
10 view orientation_change운용 최
 
09 android keyboard & layout
09 android keyboard & layout09 android keyboard & layout
09 android keyboard & layout운용 최
 
07 다양한 device_대응_방법
07 다양한 device_대응_방법07 다양한 device_대응_방법
07 다양한 device_대응_방법운용 최
 
05 pending intent
05 pending intent05 pending intent
05 pending intent운용 최
 
04 activity간에 호출하기 & intent
04 activity간에 호출하기 & intent04 activity간에 호출하기 & intent
04 activity간에 호출하기 & intent운용 최
 

Plus de 운용 최 (20)

Wot(daliworks) 20131113
Wot(daliworks) 20131113Wot(daliworks) 20131113
Wot(daliworks) 20131113
 
NIPA SW Insight Report '13.08
NIPA SW Insight Report '13.08NIPA SW Insight Report '13.08
NIPA SW Insight Report '13.08
 
Agile Spirit Base On The Book "Agile Samuari"
Agile Spirit Base On The Book "Agile Samuari"Agile Spirit Base On The Book "Agile Samuari"
Agile Spirit Base On The Book "Agile Samuari"
 
Uml intro 1
Uml intro 1Uml intro 1
Uml intro 1
 
Uml intro 0
Uml intro 0Uml intro 0
Uml intro 0
 
21 application and_network_status
21 application and_network_status21 application and_network_status
21 application and_network_status
 
20 handler and_async_task
20 handler and_async_task20 handler and_async_task
20 handler and_async_task
 
18 안드로이드 리스트뷰_속도향상
18 안드로이드 리스트뷰_속도향상18 안드로이드 리스트뷰_속도향상
18 안드로이드 리스트뷰_속도향상
 
19 나만의 view 만들기
19 나만의 view 만들기19 나만의 view 만들기
19 나만의 view 만들기
 
17 adapter view & db
17 adapter view & db17 adapter view & db
17 adapter view & db
 
16 데이터 저장과 사용
16 데이터 저장과 사용16 데이터 저장과 사용
16 데이터 저장과 사용
 
15 content provider
15 content provider15 content provider
15 content provider
 
13 service 좀더
13 service 좀더13 service 좀더
13 service 좀더
 
11 tablet 대응 가이드
11 tablet 대응 가이드11 tablet 대응 가이드
11 tablet 대응 가이드
 
10 view orientation_change
10 view orientation_change10 view orientation_change
10 view orientation_change
 
09 android keyboard & layout
09 android keyboard & layout09 android keyboard & layout
09 android keyboard & layout
 
08 font size
08 font size08 font size
08 font size
 
07 다양한 device_대응_방법
07 다양한 device_대응_방법07 다양한 device_대응_방법
07 다양한 device_대응_방법
 
05 pending intent
05 pending intent05 pending intent
05 pending intent
 
04 activity간에 호출하기 & intent
04 activity간에 호출하기 & intent04 activity간에 호출하기 & intent
04 activity간에 호출하기 & intent
 

12 service

  • 2. Service란? A Service is an application component that can perform long- running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. 활용 예. • handle network transactions • play music • perform file I/O • interact with a content provider • 기타 등등 배경에서 진행.
  • 3. Service의 2가지 형태. Type 1. 호출이 되고, 그 뒤에 주어진 일을 백그라운드에서 수행하는 데몬 같 은 형태. Type 2. 메소드를 호출시 키고, 이를 다른 곳에 호출을 시켜 수행할 수 있는 원격 호출 인터페 이스 형태. 2가지 특성 모두 가질 수 있음.
  • 5. Service에서 Thread? Service는 메인 thread에서 실행이 되고 백그라운드에서 사 용자가 알수 없도록 실행되어야 하기 때문에 별도의 thread를 통해서 MediaPlayer객체를 실행합니다. 그렇지 않으면 MediaPlayer가 실 행하는 동안에 사용자가 화면상에서의 액션을 취하려면 delay가 발생할 것입니다.
  • 6. 간단하게 다시 봅시다. 별도 Thread가 아니라서, 본 Thread가 계산에 바빠서 추가적으로 화면에서 다른 화면을 눌러도 응답이 없습니다.
  • 7. 쓰레드로 분리하고 Stop 시키기. stopService
  • 8.
  • 9. Service는 별도 Thread가 아니다. A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.
  • 10. 서비스는 정지 안 시키면 .. 서비스는 명시적으로 정지를 시키거나 (stopService) 아니면 자신의 동작을 마치면 멈추게 됩니다. 그렇다면 이 경우를 생각합니다. Service가 내부적으로 Thread를 만들어서 5분에 한 번씩 메시지를 표 시하게 구현되어 있고, 이 Service가 start 된 상황입니다. 위 경우에는 App을 닫고 Home 화면으로 이동하거나 다른 App을 실 행 하더라도 지속적으로 메시지는 꾸준히 찍게 됩니다. Activity와 관 계 없이 별도 구동하기 때문입니다. 이러한 점을 명심해서, Service 생명주기를 꼬옥 유념하기 바랍니다.
  • 11. 서비스 return 값 START_STICTY 메모리부족이나 기타 상황에서 시스템이 강제로 service를 종료된 후 service가 재시작될 때 null Intent가 담긴 onStartCommand() 콜백 함수가 실행된다. 이 경우 null Intent로 호출 때의 경우를 처리해줘야 합니다. 예) 음악 재생 프로그램 경우, 백그라운드 작업이 계속되는 경우. START_NOT_STICTY 이 경우는 프로세스가 강제로 종료되었을 경우 재 시작하지 않고 종료된 상태로 남게 됩니다. 특 정 동작이나 명령을 처리하기 위해 시작되는 서비스에 쓰입니다. 이런 서비스들은 보통 해당 명 령을 처리한 뒤 stopSelf를 이용해 종료하는 경우가 대다수. 예) App 업데이트 서비스 경우에, 리소스 경합인 현재에 다시 또 시도하지 않고 후일을 기약. START_REDELIVER_INTENT 이 경우에는 프로세스가 강제로 종료되었을 경우 Intent가 다시 전달되어 재시작합니다. 단, 여 러 차례 시도후 작업이 종료되지 않으면 service는 재시작 되지 않습니다. 반드시 실행되어야 하 는 service에 해당이 됩니다
  • 12. Service 다중 호출 startService 연속 호출 테스트 결과
  • 13. IntentService라는 것도 있어요. Service라는 것이 UI가 없이 뒤에서 도는 것이라고 흔히 이야기를 하지만, 메세지를 적절히 받아서 매번 처리를 해 나가야 합니다. 이를 위해서, 메시지를 받을 때 마다 원하는 작업을 하기 위해서는 Thread에 메시지가 들어오는 것을 모니터링하고, 들어올 때 처리를 해야 하지요. (이게 android에서 많이 나오는 handler 라고 하는 부분과 동일 형태입니다.) 이러한 것을 별도로 구현을 하는 것은 번거러울 수 있습니다. 이를 단순하 게 모두 구현해 둔 것이 IntentService입니다. 메시지 (Intent) 받아서 처리하는 형태를 추상화. 구조는 다음 페이지 참고
  • 14. IntentService Sample. BroadCastReceiver 형태로 구현도 가능. 관련 참고: http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/ Receiver는 10 초내에 작업을 마쳐야 합니다. 그렇기에 위 내용 중에서 IntentService 대신 BroadcastReceiver를 사용이 가능한 작업은 단순 작업일 때에만 하도록해야 하는 점 꼬옥 주의 합시다