Android/Custom Framework

Android 사용자 정의 Framework Service 만들기

임베디드 친구 2025. 5. 26. 21:59
728x90
반응형

Android 사용자 정의 Framework Service 만들기

Android에서 시스템 레벨의 기능을 제공하는 서비스는 대부분 Framework Service로 구현됩니다. 기본적으로 Android는 다양한 시스템 서비스를 제공하지만, 특정 기능을 추가하거나 확장하기 위해서는 사용자 정의 Framework Service를 직접 구현해야 합니다.

이번 글에서는 사용자 정의 Framework Service 구현 과정을 자세히 설명하고, AIDL 인터페이스 정의부터 SystemServer에 등록하는 과정까지 단계별로 정리하겠습니다.

1. 사용자 정의 Framework Service 개요

Framework Service는 Android의 SystemServer 프로세스에서 실행되며, 앱이나 시스템 구성 요소가 Context.getSystemService() 를 통해 접근할 수 있습니다. 주요 단계는 다음과 같습니다.

  1. AIDL 인터페이스 정의
  2. 서비스 구현 및 Binder 등록
  3. SystemServer에서 서비스 등록
  4. Context.getSystemService()를 이용한 접근

2. AIDL 인터페이스 정의

사용자 정의 Framework Service는 AIDL (Android Interface Definition Language) 을 사용하여 인터페이스를 정의해야 합니다. AIDL을 활용하면 프로세스 간 통신(IPC)이 가능하며, Binder를 통해 클라이언트와 서비스 간 데이터를 교환할 수 있습니다.

2.1 AIDL 파일 생성

먼저, frameworks/base/core/java/com/example/service/ 경로에 AIDL 파일을 생성합니다.

// IYourService.aidl
package com.example.service;

interface IYourService {
    void yourMethod();
}

AIDL 파일을 만들면 Android 빌드 시스템이 이를 바탕으로 Binder Stub 클래스 를 자동 생성합니다.

3. YourService.java에서 AIDL 구현 및 Binder 등록

AIDL 인터페이스가 정의되었으면 이를 구현하는 YourService.java 클래스를 작성해야 합니다.

package com.example.service;

import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class YourService extends IYourService.Stub {
    private static final String TAG = "YourService";

    @Override
    public void yourMethod() throws RemoteException {
        Log.d(TAG, "yourMethod() 호출됨");
    }
}

이제 YourServiceIYourService AIDL 인터페이스를 구현하는 Binder 서비스 가 되었습니다.

4. SystemServer에서 사용자 정의 서비스 등록

Framework Service는 SystemServer.java 에 등록해야 합니다. SystemServer는 Android의 핵심 시스템 서비스들을 초기화하는 역할을 합니다.

4.1 SystemServer.java 수정

frameworks/base/services/java/com/android/server/SystemServer.java 파일을 열고 run() 메서드에서 서비스를 등록합니다.

import com.example.service.YourService;

private void startOtherServices() {
    try {
        Slog.i("SystemServer", "Starting YourService");
        YourService yourService = new YourService();
        ServiceManager.addService("your_service", yourService);
    } catch (Throwable e) {
        Slog.e("SystemServer", "Failure starting YourService", e);
    }
}

이제 YourServiceyour_service 라는 이름으로 SystemServer에 등록됩니다.

5. Context.getSystemService()를 활용한 서비스 접근

사용자 정의 Framework Service가 등록되었으면, 이제 앱에서 이를 호출할 수 있습니다.

5.1 서비스 매니저를 통해 접근

import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import com.example.service.IYourService;

public class YourServiceClient {
    public static void main(String[] args) {
        IBinder binder = ServiceManager.getService("your_service");
        IYourService yourService = IYourService.Stub.asInterface(binder);

        try {
            yourService.yourMethod();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

5.2 Context.getSystemService() 활용

System API로 제공하려면 Context.getSystemService()를 활용해야 합니다. SystemServiceRegistry.java를 수정하여 서비스 매니저에 추가합니다.

registerService("your_service", YourService.class, new YourService());

그 후, 앱에서 Context.getSystemService("your_service") 를 사용하여 호출할 수 있습니다.

6. 정리

이제까지 사용자 정의 Framework Service를 만드는 과정을 살펴보았습니다.

  1. AIDL을 이용하여 서비스 인터페이스를 정의
  2. 서비스 구현 및 Binder 등록
  3. SystemServer에서 서비스 등록
  4. Context.getSystemService()로 접근

이 과정을 통해 Android 시스템 레벨에서 사용자 정의 서비스 를 추가할 수 있습니다. 실제 프로젝트에서는 보안 정책 및 SELinux 설정도 고려해야 하므로, SELinux 관련 정책 설정도 함께 적용하는 것이 좋습니다.

반응형