Android/정리2009. 5. 6. 15:59
Android Application을 개발하기 위해서는 다음 4가지 컴포넌트에 대해 알아야 한다.
 1. Activity
 2. Service
 3. Provider
 4. Receiver
 5. Manifest
이 외에 activity들의 스택인 task에 대해 알아야 한다.

1. Activity
 하나의 가상의 사용자 인터페이스에 대한 표현이다.
 - 화면에 나타나지 않을수 있고
 - 화면에 떠있을 수 있고
 - 어떤 값을 리턴할수 있고
 - 어떤 화면의 일부로 임베디드될수 있다.

 package com.android.myactivity;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

2. Service
 - 백그라운드로 수행되는 보이지 않는 클래스들이다.
 - Application process의 일부 또는 그 자체의 process로 동작할 수 있다.
 - 자신의 process또는 다른 process의 service에 bind할수 있다.
 - IDL로 정의된 remote-able interface로 한번 bind된 service와 통신이 가능하다.
 package com.android.myservice;
import android.app.Service;
public class MyService extends Service
{   
    public void onCreate() {
        Thread st = new Thread() {
            void run() { /* ... */ }
        };
        st.start();
    }
    public void onDestroy() {
        /*
        / ... *//
    }
}

3. Broadcast Receiver
 - Broadcast receiver는 broadcast announcement를 받아서 응답하는 일만 하는 다른 일은 아무것도 하지 않는 컴포넌트이다.
 - system에서 기본적으로 제공하는 많은 broadcast메세지들이 있다(예, 타임존의 변경, 배터리의 부족, 사진이 캡쳐되었는지, 사용자가 언어를 바꾸었는지등 ...)
 - Application에서 broadcast를 정의하여 발생시킬수도 있다(예, 어떤 데이터가 모두 다운되었는지 다른 app에 알리고자 할때)
 - 하나의 Application은 여러개의 broadcast receiver를 가질수 있다. receiver는 BroadcastReceiver base class를 파생시켜 작성한다.
 - Broadcast receiver는 UI를 가지고 있지 않으며 수신한 정보에 대한 응답으로 특정 activity를 시작한다거나 NotificationManager로 사용자에게 통지를 준다.
 - Notification은 다양한 방법으로 사용자에게 통지를 한다 - flashing the backlight, device떨림, 소리재생 등등...
 - 일반적으로 상태바에 아이콘으로 표시되며 사용자가 열어볼수 있게 한다.
 package org.kandroid.helloandroid;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
    }
}

4. Content provider
 - Application간에 특정 데이터를 공유할 수 있도록 해줌.
 - 획일화된 API를 제공
 - Content는 URI와 MIME type으로 표현됨.
 package org.kandroid.helloandroid;
import android.content.*;
import android.database.Cursor;
import android.net.Uri;
public class MyProvider extends ContentProvider {
    @Override public int delete(Uri arg0, String arg1, String[] arg2)
                            { return 0; }
    @Override public String getType(Uri arg0) {return null;}
    @Override public Uri insert(Uri arg0, ContentValues arg1)
                            { return null;}
    @Override public boolean onCreate() {return false;}
    @Override public Cursor query(Uri arg0, String[] arg1, String arg2,
                            String[] arg3, String arg4) {return null;}
                                        3
    @Override public int update(Uri arg0, ContentValues arg1, String arg2,
                            String[] arg3) {return 0;}
}


5. Component의 활성화
  - Content provider : ContentResolver에 의해 타켓팅될 때 활성화된다.
  - 나머지 3가지 component(Activity, Service, Receiver)는 intent로 불리는 비동기적인 메세지에 의해 활성화된다.
  - Activity, Service를 위한 Intent는 Action과 URI정보가 필요하다.
  - Broadcast receiver를 위한 intent는 action정보가 필요하다.

각 component type별로 활성화하는 방법이 있다.
  - Activity : Content.startActivity() 또는 Activity.startActivityForResult()에 intent object를 넘겨서 activity를 활성화한다.
  - Service : Context.startService()에 intent object를 넘겨서 service를 활성화한다. Android는 Service의 onStart()메소드를 호출하고 intent object를 넘긴다.
  - Broadcast Receiver : Context.sendBroadcast(), Context.sendOrderedBroadcast(), Context.sendStickyBroadcast()에 intent object를 넘겨서 broadcast를 활성화한다.

6. Component의 비활성화
 - Content provider : ContentResolver의 request에 대하여 responding중에만 활성화된다.
 - Broadcast receiver : Broadcast message에 대한 responding중에만 활성화 된다.
 - 따라서 provider와 receiver는 명시적으로 component를 비활성화 할 필요가 없다.
 - Activity, Service : idle time에도 무언가 process가 이루어지는 컴포넌트이다.
 - Activity : finish()메소드로 비활성화한다. 특정 activity에서 다른 activity를 죽이려면 finishActivity()를 호출한다.
 - Service : stopService() 또는 Context.stopService()로 비활성화한다.

7. Manifest file
 - Android가 application component를 시작하기 전에 그 component가 존재하는지에 대해 먼저 알아야 한다. 그러므로 application component들에 대한 정보를 manifest 파일에 선언하고 Android package(.Apk) file에 applicaiton code, files, resources와 함께 묶음으로 담겨 있다.
 - XML file형식
 - 모든 application에 AndroidManifest.xml로 이름이 고정되어 있슴
 - component에 대한 기술 외에 library, permission등에 대한 정보도 선언됨.
 <manifest>
    <instrumentation>
    <uses-sdk>
    <uses-permission>
      uses permission
    <permission>
    <permission-group>
    <permission-tree>
    <application>
       pp
          <uses-library>
          <activity>
          <activity-alias>
          <provider>
           p
                      <grant-uri-permission>
          <receiver>
          <service>
               <intent-filter>
                      <action>
                      <category>
                      <data>
               <meta-data>




Posted by 삼스