Android6.0对存储这块的改动还是比较大的,前面我们讲过了Android5.1上相关的代码,这里我们主要讲下区别,至于相同的部分,比如通信部分就不讲了,大家感兴趣可以看我之前5.1MountService和vold相关的博客。
一、MountService实例化
我们先从MountService的实例化开始:
mSystemServiceManager.startService(MOUNT_SERVICE_CLASS);//这个是MountService的lifecycle静态类
在SystemServer中调用了上面函数,
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) {
...........
// Register it.
mServices.add(service);//放入自己列表
// Start it.
try {
service.onStart();//调用onStart方法
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + name
+ ": onStart threw an exception", ex);
}
return service;
}
在SystemServiceManager的
startService函数中我们看到,是将Service实例化,然后放入自己维护的一个列表,最后调用了Service的onstart函数。
public static class Lifecycle extends SystemService {
private MountService mMountService;
public Lifecycle(Context context) {
super(context);
}
@Override
public void onStart() {
mMountService = new MountService(getContext());
publishBinderService("mount", mMountService);
}
@Override
public void onBootPhase(int phase) {
if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
mMountService.systemReady();
}
}
@Override
public void onStartUser(int userHandle) {
mMountService.onStartUser(userHandle);
}
@Override
public void onCleanupUser(int userHandle) {
mMountService.onCleanupUser(userHandle);
}
}
在静态类L