본문 바로가기

개발관련/안드로이드 정리 ( update file )

Create Service

BBinder                         IInterface                    BpRefBase

                                                                           

BnInterface       IServiceName      BpInterface

                                                                                   

BnServiceName                                             BpServiceName

     

ServiceName

   

안드로이드 서비스는 IPC layer, RPC layer, 서비스 layer의 3개로 이루어진 계층에서 동작이 된다.

   

서비스 요청이 발생하면 proxy와 stub 함수에서 RPC 데이터를 만들고, 만들어진 데이터를 binder 에

넘겨 주게 되면 binder 에서 IPC 통신을 이용하여 서비스가 가능하게 되도록 해준다.

   

native service

   

binder 관련 소스 /frameworks/base/libs/binder/*

binder 관련 헤더 /frameworks/base/include/binder/*

   

media service 인터페이스 소스 /frameworks/base/media/libmedia/*

media service 인터페이스 헤더 /frameworks/base/include/media/*

   

media service 소스 /frameworks/base/libs/*

media service 헤더 /frameworks/base/include/media/*

   

실제 대부분의 소스들은 IServiceName 파일에 BnServiceName, BpServiceName 을 같이 구현 되어 있음

   

서비스 구현

  • Binder, Iinterface, Service Interface stub, proxy 는 기본으로 제공
    필요한 메소드들만 오버라이딩 해 구현 해 주면 됨

   

  1. native service interface 구현
  2. service proxy, stub 구현
  3. service class 구현
     

소스파일

<<Hello_Native.zip>>

테스트 방법

$adb shell

#cd system

cd system

#cd bin

cd bin

#./helloworldservice &

./helloworldservice &

#./helloworldclient &

./helloworldclient

hello, world

   

서버와 클라이언트 실행 파일 - /frameworks/base/helloworld/hello/*

   

* android mediaserver 의 경우 /frameworks/base/media/mediaserver/main_mediaserver.cpp

에서 서버 동작 시킴

   

java service

   

native 서비스와 마찬가지로 기본적으로 제공되는 것들을 상속받아 사용
native service 의 경우 stub과 proxy, interface 를 모두 직접 작성했지만 java service는
aidl(android interface definition language)을 이용하여 interface만 작정해주면 자동으로
모든 interface 파일을 생성해줌

  • aidl /frameworks/base/tools/aidl/* ( 문법 - aidl_language_y.y )

       

java app 소스 디렉토리 /frameworks/base/core/java/android/app/*

   

서비스 구현 ( 시스템 서비스에 등록 )

  1. aidl 파일 생성
  2. service class 구현
  3. SystemServer 실행 루틴에 서비스 등록코드 추가
  4. 외부 어플리케이션에서 사용 가능하도록 래퍼 클래스 구현
  5. 컨텍스트에 서비스 획득 코드 추가

       

  • 어플리케이션에서 사용 할 때는 다음과 같이 이용하면 된다.
    JavaServiceManager jManager = (JavaServiceManager)getSystemService(Context.JAVA_SERVICE);

       

소스파일

<<Hello_Java.zip>>

디렉토리 구조

/frameworks/base/libs/helloworld/IHelloWorldService, HelloWorldService

/frameworks/base/include/helloworld/IHelloWorldService, HelloWorldService

/frameworks/base/helloworld/hello/main_helloworldservice, main_helloworldclient

   

readme.txt 파일 참조