目录
1.DevicePolicyManagerService的初始化
DevicePolicyManagerService是Android提供的一个可管理和操作设备的系统服务,通过DevicePolicyManager为应用层提供操作设备的对应策略.目前推出了三种设备管理方案:Device Administration,Profile Owner,Device Owner.
DeviceAdmin是Android2.2中引入,用户可以授权自己的应用设备管理权限后对设备进行一些简单的设置,如锁屏,禁用相机等.由于DeviceAdmin可以由用户授权的任何应用启用,因此它不支持多种企业用例,如防止删除设备管理员,设置恢复出厂设置保护等.
而Android5.0中引入了DeviceOwner和ProfileOwner模式,因此设备管理员(DeviceAdmin)已被视为旧版管理方法.由于DeviceAdmin不太适合支持当今的企业要求,因此建议从现在开始采用DeviceOwner和ProfileOwner模式来管理其设备.在Android 9.0版本中弃用了用于企业的设备管理员,并且在Android 10.0版本中将删除这些功能.
1.DevicePolicyManagerService的初始化
Android系统的启动流程大致分为启动BootLoader,Linux kernel,init进程,Zygote进程,SystemServer进程,Launcher进程.而一些java层的系统服务就是在SystemServer进程中启动,包括ActivityManagerService,WindowManagerService,PackageManagerService,DevicePolicyManagerService等.
而DevicePolicyManagerService在SystemServer中的startOtherServices()方法启动,具体如下.
类名SystemServer:
1.1.startOtherServices()
这里是调用SystemServiceManager的startService方法来启动DevicePolicyManagerService.Lifecycle
// android-29\com\android\server\SystemServer.java
private void startOtherServices() {
... ...
// Always start the Device Policy Manager, so that the API is compatible with
// API8.
traceBeginAndSlog("StartDevicePolicyManager");
mSystemServiceManager.startService(DevicePolicyManagerService.Lifecycle.class);
traceEnd();
... ...
}
类名SystemServiceManager:
1.2.startService()
在SystemServiceManager的start()方法中首先会去判断当前启动的系统服务是否继承自SystemService,否则抛出异常.
随后通过DevicePolicyManagerService.Lifecycle类对象的getConstructor()方法获取构造器对象(Constructor),并调用其newInstance()方法实例化对象;这里调用的是DevicePolicyManagerService.Lifecycle有参构造方法Lifecycle(Context context),而DevicePolicyManagerService类就是在此构造方法中实例化的.
最后继续调用SystemServiceManager的内部方法startService().
// android-29\com\android\server\SystemServiceManager.java
public <T extends SystemService> T startService(Class<T> serviceClass) {
try {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + 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);
}
startService(service);
return service;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
1.3.startService()
这个方法比较简单,首先把DevicePolicyManagerService.Lifecycle添加到mServices中,mServices是一个存储SystemService类型的ArrayList;然后调用该服务的start()方法.
// android-29\com\android\server\SystemServiceManager.java
public void startService(@NonNull final SystemService service) {
// Register it.
mServices.add(service);