1. Android components
    1. Activities
      1. Activity Life Cycle
        1. Starting state
        2. Running state
        3. Paused state
        4. Stopped state
        5. Destroyed state
    2. Intents
    3. Services
    4. Content Providers
    5. Broadcast Receivers
    6. References

Android components

앱을 만들기 위해
activities가 무엇인지, intent는 어떻게 동작하는지, service가 왜 쿨한지,
broadcast receiver와 content provider는 어떻게 쓰는지 등등 알아봅니다.

Activities

  • A single screen, 눈에 보이는 부분
  • 보통 하나의 앱은 여러 Activity를 가짐
  • 사용자는 activity 간에 flip back and forth

Activity Life Cycle

  • The activity life cycle is managed via Activity Manager.
    • creating, destroying, managing activites.
    • Older activities that the user hasn't used in a while will be destroyed in order to free more
      space for the currently active one.
    • Similar to container-based environment (Java applets or Java Servlets)
  • 다음 그림은 Activity의 상태를 보여준다.
    • 사용자에게 노출되는 것이라 state가 많음.(service보다...)


그림1. Activity Life cycle

Starting state
  • activity가 메모리에 존재하지 않는 상태
  • starting state에서 running state로 전환할 때 가장 비용이 큼.(이때 배터리도 가장 많이 소모됨)
  • 이런 이유로 activity가 노출되지 않아도 바로 자동으로 destory하지 않는다.(재활용을 위해)
Running state
  • Running state에 있는 activity는 하나이다 - 현재 스크린에 노출된 하나.
  • Interaction with the user.
    • typing, touching the screen, clicking buttons.
Paused state
  • activity가 사용자와 interaction이 없는데, 스크린에는 여전히 노출되고 있을때의 상태임.
  • 일반적인 시나리오 아님
    • 작은 디바이스 화면에서, activity는 전체 화면이거나 전혀 노출되지 않기 때문에 main flow는 아님.
  • 그럼 언제 Paused상태인가?
    • e.g. activity 위에 dialog box가 떴을 때가 Paused.
Stopped state
  • activity가 보이지 않지만 아직 메모리에 상주해 있는 상태.
  • activity는 system에 의해 보호받고 있음.
    • 왜냐하면 사용자에 의해서 언제 다시 호출될 지 알 수 없고(곧바로 호출될수도 있음),
      처음부터 activity를 실행하는 것보다 이게 휠씬 저렴하기 때문임.
  • front로 돌아오면서 Running activity가 되거나 어느 순간 메모리에서 완전 제거될 수 있음.(어떤 순간인지 알 수 없음)
Destroyed state
  • activity가 메모리에 없는 상태임
  • Activity Manager가 더 필요없다고 판단하고 제거함.

Intents

  • major build blocks 사이에 전달하는 메시지다.
  • 비동기로 메시지를 전달함.
  • 특정 액션을 수행하라는 메시지를 전달하면 이를 수행하는 앱이 있는데, 내가 원하는 다른 앱으로 대체할 수 있음.
    • e.g. url 링크를 browser가 아닌 네이버앱으로 여는 게 가능함.
  • 다음 그림은 다양한 activity 간 "jump" 하는 것을 보여줌.
    (같은 앱 내이거나 다른 앱으로의)


그림2. Intents

Services

  • Service는 background에서 동작하고 UI(User Interface)를 가지지 않는 컴포넌트다.
  • 화면없이 동작시킬 때 아주 유용하다.
    • e.g. music player의 재생 기능. 다른 앱 이용 중에도 재생 가능함.
  • 서비스의 life cycle은 굉장히 간단함.(activity에 비해..)


그림3. Service life cycle

Be Careful

The fact that a service runs in the background doesn't mean it runs on
a separate thread. If a service is doing some processing that takes a while
to complete (such as performing network calls), you would typically run
it on a separate thread. Otherwise, your user interface will run noticeably
slower. In other words, services and activities run on the same main
application thread, often called the UI thread.

Content Providers

  • 앱 간 data를 공유하기 위한 인터페이스다.
  • 앱에 속하는 모든 데이터가 시스템의 다른 앱에서 완전히 격리될 수 있도록 기본적으로 안드로이드에서는 자체 샌드박스 내에서 앱을 각각 실행한다.
    (e.g. 카페앱과 블로그앱의 데이터 공유 불가)
  • Intent를 통해서 소량의 데이터를 앱 간에 전달할 수 있지만, Content Provider는 큰 데이터셋 사이에 persistent data를 공유하기에 휠씬 적합하다.
  • CRUD principle을 잘 준수한다.
    • Standard database 메소드와 많이 비슷하고 database proxy 이므로 구현이 상대적으로 쉬움


그림 4. Content provider

e.g.
1) Contact Provider(연락처 제공자)는 다양한 앱에 모든 사용자의 연락처 데이터를 제공하는 content provider임.
2) Settings Provider exposes system settings to various applications, including the built-in Settings application.
3) Media Store는 다양한 앱에서 사진과 음악과 같은 다양한 미디어를 저장하고 공유함.

  • 컨택츠 앱(e.g.주소록앱)은 Content Provider를 이용한다.(완전히 분리된 application)
    content provider는 사용자의 연락처를 전달해준다.
  • 컨택츠 앱은 자체적으로 연락처 데이터를 가지지 않고 Content Provider는 UI가 없다.


그림 5. Contacts applicationn using Contacts Provider to get the data

Broadcast Receivers

  • publish/subscribe mechanism, 더 정확하게 말하면 Observer pattern 의 안드로이드 구현체
  • system은 항상 이벤트를 broadcast함.
    • SMS도착알림, low battery상태, 시스템 부트 등의 모든 이벤트는 broadcaste되고 있음.
    • 이런 이벤트를 받고자 하는 곳에 알려주는 기능을 한다.

References

Learning Android, Marko Gargenta O'REILLY
Gesture