上面已经说了很多了,这里补充一点:为什么 bindService 能和 Activity 的生命周期联动?
这个需要到ContextImpl里面看下源码:
@Override
public boolean bindService(Intent service, ServiceConnection conn,
int flags) {
warnIfCallingFromSystemProcess();
return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), getUser());
}
最终下去会发现给一个LoadedApk里面,里面使用了一个名叫mService的Map将Serivice的信息存储起来了,然后在ActivityThead的handleDestroyActivity方法里面找到下面的代码的:
Context c = r.activity.getBaseContext();
if (c instanceof ContextImpl) {
((ContextImpl) c).scheduleFinalCleanup(
r.activity.getClass().getName(), "Activity");
}
追踪下去就会到H类的处理HanlderMessage里面:
case CLEAN_UP_CONTEXT:
ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
cci.context.performFinalCleanup(cci.who, cci.what);
break;
回到ContextImpl:
final void performFinalCleanup(String who, String what) {
//Log.i(TAG, "Cleanup up context: " + this);
mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
}
回到了LoadedApk,在removeContextRegistrations方法里面有这么一段代码:
synchronized (mServices) {
//Slog.i(TAG, "Receiver registrations: " + mReceivers);
**ArrayMap smap =
mServices.remove(context);**
if (smap != null) {
for (int i = 0; i < smap.size(); i++) {
LoadedApk.ServiceDispatcher sd = smap.valueAt(i);
ServiceConnectionLeaked leak = new ServiceConnectionLeaked(
what + " " + who + " has leaked ServiceConnection "
+ sd.getServiceConnection() + " that was originally bound here");
leak.setStackTrace(sd.getLocation().getStackTrace());
Slog.e(ActivityThread.TAG, leak.getMessage(), leak);
if (reportRegistrationLeaks) {
StrictMode.onServiceConnectionLeaked(leak);
}
try {
ActivityManager.getService().unbindService(
sd.getIServiceConnection());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
sd.doForget();
}
}
mUnboundServices.remove(context);
//Slog.i(TAG, "Service registrations: " + mServices);
}
看到这里的吗:
ActivityManager.getService().unbindService(
sd.getIServiceConnection());
总结就是bindService会让系统保存该Service的信息并在ActivtyThread里面监听Actiivty的销毁,通过一个for循环找出需要解除绑定的Serice来进行解绑