这是在android 6.0下的实现,其他版本可能有改动
在Context.java添加服务名称
//--->frameworks/base/core/java/android/content/Context.java
public static final String A_SERVICE = "a";
@StringDef {
...
A_SERVICE
}
在ServiceServicer注册服务
//--->frameworks/base/services/java/com/android/server/SystemServer.java
private void startOtherServices() {
...
ServiceManager.addService("AService", new AService);
...
}
在ContextImpl.java添加获取服务管理接口
//--->frameworks/base/core/java/android/app/ContextImpl.java
static {
....
registerService(Context.A_SERVICE,AManager.class,
new CachedServiceFetcher<AManager>() {
@Override
public AManager createService(ContextImpl ctx) {
return new AManager(ctx);
}});
}
Service的aidl
interface IAExService {
}
Service 本体
public class AService extends IAService.Stub {
}
Manager
public class AManager {
public AManager(Context context) {
}
static public IAService getService()
{
IBinder b = ServiceManager.getService("notification");
return IAService.Stub.asInterface(b);
}
}