onStart
private void start() {
connectStoraged();
connectVold();
}
private void connectStoraged() {
IBinder binder = ServiceManager.getService(“storaged”);
if (binder != null) {
try {
// 设置死亡代理以重新建立连接
binder.linkToDeath(new DeathRecipient() {
@Override
public void binderDied() {
Slog.w(TAG, “storaged died; reconnecting”);
mStoraged = null;
connectStoraged();
}
}, 0);
} catch (RemoteException e) {
binder = null;
}
}
if (binder != null) {
// 获取storaged的bp端用于通信
mStoraged = IStoraged.Stub.asInterface(binder);
} else {
Slog.w(TAG, “storaged not found; trying again”);
}
if (mStoraged == null) {
BackgroundThread.getHandler().postDelayed(() -> {
connectStoraged();
}, DateUtils.SECOND_IN_MILLIS);
} else {
onDaemonConnected();
}
}
private void connectVold() {
IBinder binder = ServiceManager.getService(“vold”);
if (binder != null) {
try {
// 设置死亡代理以重新建立连接
binder.linkToDeath(new DeathRecipient() {
@Override
public void binderDied() {
Slog.w(TAG, “vold died; reconnecting”);
mVold = null;
connectVold();
}
}, 0);
} catch (RemoteException e) {
binder = null;
}
}
if (binder != null) {
// 获取Vold的bp端用于通信
mVold = IVold.Stub.asInterface(binder);
try {
// 关键代码:设置Vold的Listener
mVold.setListener(mListener);
} catch (RemoteException e) {
mVold = null;
Slog.w(TAG, “vold listener rejected; trying again”, e);
}
} else {
Slog.w(TAG, “vold not found; trying again”);
}
if (mVold == null) {
BackgroundThread.getHandler().postDelayed(() -> {
connectVold();
}, DateUtils.SECOND_IN_MILLIS);
} else {
onDaemonConnected();
}
}
connectStoraged和connectVold分别是获取Vold和Storaged服务的bp端,设置死亡代理, 为Vold设置Listener监听,然后调用onDaemonConnected
public void onDaemonConnected() {
mDaemonConnected = true;
mHandler.obtainMessage(H_DAEMON_CONNECTED).sendToTarget();
}