android 有关怎么自己添加系统级服务 java层 重点aidl

本文深入探讨了Android系统中`frameworks/base/core/java/android/os`目录下的`aidl`文件与`frameworks/base/services/java/com/android/server`目录下的`aidl`文件的区别,并详细解释了如何通过`ServiceManager`实现服务的注册与调用过程。包括对关键类`CarSignalManagerService`及其使用方式的解析,以及`ServiceManager.addService`方法背后的逻辑。同时,对比了`IServiceManager`的两种实现方式——`ServiceManagerNative`与`ServiceManagerProxy`,并阐述了它们在服务调用中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

frameworks/base/core/java/android/os下面加aidl文件和frameworks/base/core/java/android/app下面加aidl文件有什么区别,不清楚

interface ICarSignalManager { void setBacklightState(boolean state); }
在frameworks/base/services/java的com.android.server下面

public class CarSignalManagerService extends ICarSignalManager.Stub { ...... }
在SystemServer的ServerThread的run方法中

ServiceManager.addService(Context.CAR_SIGNAL_SERVICE, new CarSignalManagerService(context));
上面的方法涉及到ServiceManager,我们这里根据那个addService来跟踪下:

public static void addService(String name, IBinder service) { try { getIServiceManager().addService(name, service); } catch (RemoteException e) { Log.e(TAG, "error in addService", e); } }
IServiceManager的实现类:ServiceManagerNative和ServiceManagerProxy这两个又有什么区别呢?

服务调用的地方

public CarSignalManager(Context context) { IBinder b = ServiceManager.getService(Context.CAR_SIGNAL_SERVICE); if (b == null) { throw new RuntimeException("Car Signal service not available!"); } mService = ICarSignalManager.Stub.asInterface(b); }
CarSignalManager carSignalManager = (CarSignalManager) mContext .getSystemService(Context.CAR_SIGNAL_SERVICE); carSignalManager.setBacklightState(false);

ApplicationContext中

@Override public Object getSystemService(String name) { ... if (CAR_SIGNAL_SERVICE.equals(name)) { return getCarSignalManager(); } ... }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值