SystemService – 所有系统服务的父类
-
系统服务的构造方法:
public SystemService(Context context) {//需要传递一个Context变量来构造,SystemService又为这个context提供了get()方法 mContext = context; }
-
提供Context和UiContext:
public final Context getContext() { return mContext; } public final Context getUiContext() {//UiContext是从当前活动所在线程获取的 return ActivityThread.currentActivityThread().getSystemUiContext(); }
-
定义了两种服务类型:
protected final void publishBinderService(...){//第一种:BinderService,这种服务可以被其他服务或者应用访问 ServiceManager.addService(...);//开启BinderService的方式就是把服务加到ServiceManager中以获取binder通讯的能力 } protected final void publishLocalService(...) {//第二种:LocalService,这种服务不可以被其他服务或者应用访问 LocalServices.addService(...);//加入到LocalService中 }
LocalService的作用和ServiceManager类似,用来保存那些不需要进行binder通信的服务
常见的SystemService
-
继承自SystemService的类很多很多,使用
grep -r 'extends SystemService'
在framework中一查便知