[Android] Android 7 SystemService Add: 추가 방법

Android Service는 백그라운드에서 실행되는 process를 말한다. (상세 내용)

AOSP에서 Android SystemService를 추가하는 방법은 다음과 같다.

Service Manager 생성

[ANDROID_PATH]/frameworks/base/[MyServicePath]/ 에서

MyserviceManager.java 생성

 

AIDL 생성

[ANDROID_PATH]/frameworks/base/[MyServicePath]/ 에서

IMyServiceManager.aidl 생성

 

[ANDROID_PATH]/frameworks/base/Android.mk 에서

  1. [MyServicePath]/IMyServiceManager.aidl 추가
## READ ME: ########################################################
##
## When updating this list of aidl files, consider if that aidl is
## part of the SDK API. If it is, also add it to the list below that
## is preprocessed and distributed with the SDK. This list should
## not contain any aidl files for parcelables, but the one below should
## if you intend for 3rd parties to be able to send those objects
## across process boundaries.
##
## READ ME: ########################################################
LOCAL_SRC_FILES += \
 core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl \
 [MyServicePath]/IMyServiceManager.aidl

 

Service 생성

[ANDROID_PATH]/frameworks/base/services/core/java/com/android/server/ 에서

  1. MyService.java 생성

 

ServiceManager에 추가

[ANDROID_PATH]/frameworks/base/core/java/android/content/Context.java 에서 다음을 추가

public abstract boolean startInstrumentation(@NonNull ComponentName className,
            @Nullable String profileFile, @Nullable Bundle arguments);

    /** @hide */
    @StringDef({
            POWER_SERVICE,
...
            MY_SERVICE,

...


 /**
 * Return the handle to a system-level service by name. The class of the
 * returned object varies by the requested name. Currently available names
 * are:
 *
...
 * <dt> {@link #MY_SERVICE} ("myservice")
 * <dd> A {@link my.service.MyServiceManager MyServiceManager}

...
 * @see #HARDWARE_PROPERTIES_SERVICE
 * @see #MY_SERVICE
 * @see my.service.MyServiceManager

...

 /**
 * Use with {@link #getSystemService} to retrieve a {@link
 * my.service.MyServiceManager}
 *
 * @see #getSystemService
 * @see my.service.MyServiceManager
 */
 public static final String MY_SERVICE = "myservice";

 

[ANDROID_PATH]/frameworks/base/services/java/com/android/server/SystemService.java 에서 다음을 추가

ConnectivityService connectivity = null;
MyService myService = null;

...


traceBeginAndSlog("StartMyService");
try {
    myService = new MyService(context);
    ServiceManager.addService(Context.MY_SERVICE, myService);
} catch (Throwable e) {
    reportWtf("starting My Service", e);
}
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

 

[ANDROID_PATH]/frameworks/base/core/java/android/app/SystemServiceRegistry.java 에서 다음을 추가

import my.service.IMyServiceManager;
import my.service.MyServiceManager;

...


 return new ContextHubManager(ctx.getOuterContext(),
 ctx.mMainThread.getHandler().getLooper());
 }});

registerService(Context.MY_SERVICE, MyServiceManager.class,
    new CachedServiceFetcher<MyServiceManager>() {
@Override
public MyServiceManagercreateService(ContextImpl ctx) {
    IBinder b = ServiceManager.getService(Context.MY_SERVICE);
    IMyServiceManager service = IMyServiceManager.Stub.asInterface(b);
    return new MyServiceManager(ctx.getOuterContext(), service);
}});

 

 

Application 에서 사용

> make sdk

로 빌드 후

 

[ANDROID_PATH]/target/common/obj/PACKAGING/android_jar_intermediates/android.jar

혹은

[ANDROID_PATH]/out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar
[ANDROID_PATH]/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
파일을 Application 개발환경의 SDK로 포함한 후 사용하면 된다.
이상으로 android systemservice 추가 방법을 마친다.