整个MountService和vold是使用NativeDaemonConnector,今天分析下:
在MountService的构造函数中有如下:
mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG, 25,
null);//新建NativeDaemonConnector
Thread thread = new Thread(mConnector, VOLD_TAG);
thread.start();
看看NativeDaemonConnector的构造函数,MountService 是callbacks回调,"vold"是socket的名字
NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket,
int responseQueueSize, String logTag, int maxLogSize, PowerManager.WakeLock wl) {
this(callbacks, socket, responseQueueSize, logTag, maxLogSize, wl,
FgThread.get().getLooper());
}
NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket,
int responseQueueSize, String logTag, int maxLogSize, PowerManager.WakeLock wl,
Looper looper) {
mCallbacks = callbacks;
mSocket = socket;
mResponseQueue = new ResponseQueue(responseQueueSize);
mWakeLock = wl;
if (mWakeLock != null) {
mWakeLock.setReferenceCounted(true);
}
mLooper = looper;
mSequenceNumber = new AtomicInteger(0);
TAG = logTag != null ? logTag : "NativeDaemonConnector";
mLocalLog = new LocalLog(maxLogSize);
}
又因为NativeDaemonConnector 继承Runnable,所以在MountService中开启一个线程,会执行NativeDaemonConnector 的run函数
final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdog.Monitor {
NativeDaemonConnector 的run函数
public void run() {
mCallbackHandler = new Handler(mLooper, this);
while (true) {
try {
listenToSocket();
} catch (Exception e) {
loge("Error in NativeDaemonConnector: " + e);
SystemClock.sleep(5000);
}
}
}
再来看看listenToSocket函数
private void listenToSocket() throws IOException {
LocalSocket