Android SystemService的启动流程
来源:互联网 发布:淘宝海狗鞭 编辑:程序博客网 时间:2024/04/30 07:20
SystemServer.java(由init进程起来)之后会调用:
// Initialize the system context.
createSystemContext();<strong>//方法一
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
//ActivityManagerService 启动
mActivityManagerService = mSystemServiceManager.startService(//方法二
ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
//方法一
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
}
//方法二
public <T extends SystemService> T startService(Class<T> serviceClass) {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
// Register it.
mServices.add(service);
// Start it.
try {
service.onStart();//方法三,很关键的方法,会把自己添加到SystemManager中
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + name
+ ": onStart threw an exception", ex);
}
return service;
}
//方法三
@Override
public void onStart() {
publishBinderService(Context.FINGERPRINT_SERVICE, new FingerprintServiceWrapper());//方法四
IFingerprintDaemon daemon = getFingerprintDaemon();
if (DEBUG) Slog.v(TAG, "Fingerprint HAL id: " + mHalDeviceId);
listenForUserSwitches();
}
//方法四
/**
* Publish the service so it is accessible to other services and apps.
*/
protected final void publishBinderService(String name, IBinder service,
boolean allowIsolated) {
ServiceManager.addService(name, service, allowIsolated);
}
//authenticate()为什么要用mhander.post 调用startAuthentication()方法,不可以直接调用
mHandler.post(new Runnable() {
@Override
public void run() {
startAuthentication(token, opId, effectiveGroupId, receiver, flags, restricted);
}
});
// Initialize the system context.
createSystemContext();<strong>//方法一
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
//ActivityManagerService 启动
mActivityManagerService = mSystemServiceManager.startService(//方法二
ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
//方法一
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
}
//方法二
public <T extends SystemService> T startService(Class<T> serviceClass) {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
// Register it.
mServices.add(service);
// Start it.
try {
service.onStart();//方法三,很关键的方法,会把自己添加到SystemManager中
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + name
+ ": onStart threw an exception", ex);
}
return service;
}
//方法三
@Override
public void onStart() {
publishBinderService(Context.FINGERPRINT_SERVICE, new FingerprintServiceWrapper());//方法四
IFingerprintDaemon daemon = getFingerprintDaemon();
if (DEBUG) Slog.v(TAG, "Fingerprint HAL id: " + mHalDeviceId);
listenForUserSwitches();
}
//方法四
/**
* Publish the service so it is accessible to other services and apps.
*/
protected final void publishBinderService(String name, IBinder service,
boolean allowIsolated) {
ServiceManager.addService(name, service, allowIsolated);
}
//authenticate()为什么要用mhander.post 调用startAuthentication()方法,不可以直接调用
mHandler.post(new Runnable() {
@Override
public void run() {
startAuthentication(token, opId, effectiveGroupId, receiver, flags, restricted);
}
});
0 0
- Android SystemService的启动流程
- [Android 系统源代码研究] SystemService启动
- Android 的启动流程
- android启动的流程
- Android 的启动流程
- Android 的启动流程
- android的启动流程
- android的启动流程
- Android 之 SystemService
- Android-Adding SystemService
- Android-Adding SystemService
- Android 之 SystemService
- Android开发之SystemService
- Android 之 SystemService
- Android 之 SystemService
- Android 之 SystemService
- Android 之 SystemService
- Android SystemService初体验
- Java出现No enclosing instance of type E is accessible
- 313. Super Ugly Number
- 计算几何
- easyUi--Uploadify文件上传
- 获取泛型中的数据并转换为数组
- Android SystemService的启动流程
- Java中equals和==的区别
- 0x00D83DDA 处有未经处理的异常(在 DXFont.exe 中): 0xC000041D: 用户回调期间遇到未经处理的异常
- MFC笔记-小实例-分割窗口-源代码
- 查验身份证(水)
- leetcode 204. Count Primes-素数统计
- 发现一个多国语言互译的非谷歌翻译网站
- java CountDownLatch测试并发数
- 布局文件中出现的错误