【Service】bindService调用流程

【Service】bindService调用流程bindService 流程

时序图

【Service】bindService调用流程

源码执行流程

【ContextImpl.java】

 @Override public boolean bindService(Intent service, ServiceConnection conn, int flags) { warnIfCallingFromSystemProcess(); return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), Process.myUserHandle()); } private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler handler, UserHandle user) { IServiceConnection sd; if (conn == null) { throw new IllegalArgumentException("connection is null"); } if (mPackageInfo != null) { sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags); //sd = android.app.LoadedApk$ServiceDispatcher$InnerConnection@8df116d } else { throw new RuntimeException("Not supported in system context"); } validateServiceIntent(service); try { IBinder token = getActivityToken(); if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null && mPackageInfo.getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { flags |= BIND_WAIVE_PRIORITY; } service.prepareToLeaveProcess(this); //经过Binder调用到AMS.bindService int res = ActivityManager.getService().bindService( mMainThread.getApplicationThread(), getActivityToken(), service, service.resolveTypeIfNeeded(getContentResolver()), sd, flags, getOpPackageName(), user.getIdentifier()); if (res < 0) { throw new SecurityException( "Not allowed to bind to service " + service); } return res != 0; } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }

【ActivityManagerService.java】

public int bindService(IApplicationThread caller, IBinder token, Intent service, String resolvedType, IServiceConnection connection, int flags, String callingPackage, int userId) throws TransactionTooLargeException { enforceNotIsolatedCaller("bindService"); //参数经过Binder调用connection=android.app.IServiceConnection$Stub$Proxy@ // Refuse possible leaked file descriptors if (service != null && service.hasFileDescriptors() == true) { throw new IllegalArgumentException("File descriptors passed in Intent"); } if (callingPackage == null) { throw new IllegalArgumentException("callingPackage cannot be null"); } synchronized(this) { //mServices的类型是ActiveServices return mServices.bindServiceLocked(caller, token, service, resolvedType, connection, flags, callingPackage, userId); } }

【ActiveServices.java】

int bindServiceLocked(IApplicationThread caller, IBinder token, Intent service, String resolvedType, final IServiceConnection connection, int flags, String callingPackage, final int userId) throws TransactionTooLargeException { final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); if (callerApp == null) {//callerApp不为null ...//代码不执行 } ActivityRecord activity = null; if (token != null) { activity = ActivityRecord.isInStackLocked(token); if (activity == null) { //activity不为null Slog.w(TAG, "Binding with unknown activity: " + token); return 0; } } int clientLabel = 0; PendingIntent clientIntent = null; final boolean isCallerSystem = callerApp.info.uid == Process.SYSTEM_UID; //isCallerSystem = false if (isCallerSystem) { //代码不会执行 ...... } if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) { //没有设置该flag,代码不会执行 ... } if ((flags & Context.BIND_ALLOW_WHITELIST_MANAGEMENT) != 0 && !isCallerSystem) { ... //没有设置BIND_ALLOW_WHITELIST_MANAGEMENT,代码不会执行 } final boolean callerFg = callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND; final boolean isBindExternal = (flags & Context.BIND_EXTERNAL_SERVICE) != 0; //callerFg = true , isBindExternal = false ServiceLookupResult res = retrieveServiceLocked(service, resolvedType, callingPackage, Binder.getCallingPid(), Binder.getCallingUid(), userId, true, callerFg, isBindExternal); if (res == null) { //res 不为null return 0; } if (res.record == null) { //res.record 不为null return -1; } ServiceRecord s = res.record; boolean permissionsReviewRequired = false; //mAm.mPermissionReviewRequired为false,表示不需用户手动授权 if (mAm.mPermissionReviewRequired) { ......//代码不会执行 } final long origId = Binder.clearCallingIdentity(); try { if (unscheduleServiceRestartLocked(s, callerApp.info.uid, false)) { ....// } //设置了该flag-BIND_AUTO_CREATE if ((flags&Context.BIND_AUTO_CREATE) != 0) { s.lastActivity = SystemClock.uptimeMillis(); if (!s.hasAutoCreateConnections()) { //结果为false,代码执行 // This is the first binding, let the tracker know. ServiceState stracker = s.getTracker(); if (stracker != null) { stracker.setBound(true, mAm.mProcessStats.getMemFactorLocked(), s.lastActivity); } } } mAm.startAssociationLocked(callerApp.uid, callerApp.processName, callerApp.curProcState, s.appInfo.uid, s.name, s.processName); mAm.grantEphemeralAccessLocked(callerApp.userId, service, s.appInfo.uid, UserHandle.getAppId(callerApp.uid)); AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); ConnectionRecord c = new ConnectionRecord(b, activity, connection, flags, clientLabel, clientIntent); IBinder binder = connection.asBinder(); //结果binder =android.os.BinderProxy@9df6dc7 ......//根据binder查找到的ServiceRecord,加入到各种组件的connections变量中 //设置了该flag-BIND_AUTO_CREATE if ((flags&Context.BIND_AUTO_CREATE) != 0) { s.lastActivity = SystemClock.uptimeMillis(); //!!!关键函数bringUpServiceLocked,正常结果会返回null if (bringUpServiceLocked(s, service.getFlags(), callerFg, false, permissionsReviewRequired) != null) { return 0; } } if (s.app != null) { if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) { s.app.treatLikeActivity = true; } if (s.whitelistManager) { s.app.whitelistManager = true; } // This could have made the service more important. mAm.updateLruProcessLocked(s.app, s.app.hasClientActivities || s.app.treatLikeActivity, b.client); mAm.updateOomAdjLocked(s.app, true); } //其中s.app=ProcessRecord{232a306 2264:com.example.atc6111.testdymamicreceiver/u0a59} //其中b.intent.received = false if (s.app != null && b.intent.received) { ......//代码不执行 } else if (!b.intent.requested) { requestServiceBindingLocked(s, b.intent, callerFg, false); } getServiceMapLocked(s.userId).ensureNotStartingBackgroundLocked(s); } finally { Binder.restoreCallingIdentity(origId); } return 1; }

接下来就是关键函数bringUpServiceLocked登场!

private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg, boolean whileRestarting, boolean permissionsReviewRequired) throws TransactionTooLargeException { //其中r.app = null,第一次调用bindService时该值为null,在绑定之后再调用就不为null if (r.app != null && r.app.thread != null) { //代码不执行 sendServiceArgsLocked(r, execInFg, false); return null; } if (!whileRestarting && mRestartingServices.contains(r)) {//代码不执行 // If waiting for a restart, then do nothing. return null; } //在创建服务前的准备工作: //1. 从mPendingService中把这个ServiceRecord移除 //2. 判断拥有此服务的user是否已经启动 //3. 设置Service所在的package的停止状态为false ...... final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0; //没有将service的isolatedProcess属性设置为true,所以isolated = false final String procName = r.processName; String hostingType = "service"; ProcessRecord app; if (!isolated) { app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false); //其中app=ProcessRecord{232a306 2264:com.example.maureen.testdymamicreceiver/u0a59} //其中app.thread=android.app.IApplicationThread$Stub$Proxy@e77d31d if (app != null && app.thread != null) { try { app.addPackage(r.appInfo.packageName, r.appInfo.versionCode, mAm.mProcessStats); //【关键函数!!!】执行Service的onCreate的过程 realStartServiceLocked(r, app, execInFg); return null; //执行成功这个函数就直接返回null } catch (TransactionTooLargeException e) { throw e; } catch (RemoteException e) { Slog.w(TAG, "Exception when starting service " + r.shortName, e); } } } else { //不会执行 ... } //成功执行realStartServiceLocked后,就直接返回,后面的代码都不会执行了!!!!! ...... return null; } 

再来关键函数realStartServiceLocked:

private final void realStartServiceLocked(ServiceRecord r, ProcessRecord app, boolean execInFg) throws RemoteException { if (app.thread == null) { throw new RemoteException(); } r.app = app; r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); final boolean newService = app.services.add(r); //设置超时消息 bumpServiceExecutingLocked(r, execInFg, "create"); mAm.updateLruProcessLocked(app, false, null); updateServiceForegroundLocked(r.app, /* oomAdj= */ false); //更新OOM mAm.updateOomAdjLocked(); boolean created = false; try { synchronized (r.stats.getBatteryStats()) { r.stats.startLaunchedLocked(); } mAm.notifyPackageUse(r.serviceInfo.packageName, PackageManager.NOTIFY_PACKAGE_USE_SERVICE); app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE); //!!!【关键函数】异步调用ApplicationThread.scheduleCreateService //因为是异步调用,所以不用等返回结果就可以继续往下执行 app.thread.scheduleCreateService(r, r.serviceInfo, mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo), app.repProcState); r.postNotification(); created = true; } catch (DeadObjectException e) { Slog.w(TAG, "Application dead when creating service " + r); mAm.appDiedLocked(app); throw e; } finally { if (!created) { // Keep the executeNesting count accurate. final boolean inDestroying = mDestroyingServices.contains(r); serviceDoneExecutingLocked(r, inDestroying, inDestroying); // Cleanup. if (newService) { app.services.remove(r); r.app = null; } // Retry. if (!inDestroying) { scheduleServiceRestartLocked(r, false); } } } if (r.whitelistManager) { app.whitelistManager = true; } //【又一个关键函数!】Service的onStartCommand调用过程 requestServiceBindingsLocked(r, execInFg); updateServiceClientActivitiesLocked(app, null, true); if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), null, null, 0)); } //这个函数进入之后,N = 0,所以直接返回了 sendServiceArgsLocked(r, execInFg, true); if (r.delayed) { getServiceMapLocked(r.userId).mDelayedStartList.remove(r); r.delayed = false; } if (r.delayedStop) { // Oh and hey we've already been asked to stop! r.delayedStop = false; if (r.startRequested) { if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "Applying delayed stop (from start): " + r); stopServiceLocked(r); } } } 

好的,上面又又又又提到了两个关键函数:ApplicationThread.scheduleCreateService、requestServiceBingingsLocked。

ApplicationThread.scheduleCreateService

【ActivityThread.java】

ActivityThread.ApplicationThread

 public final void scheduleCreateService(IBinder token, ServiceInfo info, CompatibilityInfo compatInfo, int processState) { updateProcessState(processState, false); CreateServiceData s = new CreateServiceData(); s.token = token; s.info = info; s.compatInfo = compatInfo; sendMessage(H.CREATE_SERVICE, s); }

ActivityThread

 final H mH = new H(); private void sendMessage(int what, Object obj) { sendMessage(what, obj, 0, 0, false); } private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) { Message msg = Message.obtain(); msg.what = what; msg.obj = obj; msg.arg1 = arg1; msg.arg2 = arg2; if (async) { msg.setAsynchronous(true); } mH.sendMessage(msg); }

ActivityThread.H

public void handleMessage(Message msg) { switch (msg.what) { ... case CREATE_SERVICE: Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceCreate: " + String.valueOf(msg.obj))); handleCreateService((CreateServiceData)msg.obj); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); break; ... } }

ActivityThread

private void handleCreateService(CreateServiceData data) { unscheduleGcIdler(); LoadedApk packageInfo = getPackageInfoNoCheck( data.info.applicationInfo, data.compatInfo); Service service = null; try { java.lang.ClassLoader cl = packageInfo.getClassLoader(); service = (Service) cl.loadClass(data.info.name).newInstance(); } catch (Exception e) { if (!mInstrumentation.onException(service, e)) { throw new RuntimeException( "Unable to instantiate service " + data.info.name + ": " + e.toString(), e); } } try { ContextImpl context = ContextImpl.createAppContext(this, packageInfo); context.setOuterContext(service); Application app = packageInfo.makeApplication(false, mInstrumentation); service.attach(context, this, data.info.name, data.token, app, ActivityManager.getService()); //调用自定义Service的onCreate函数 service.onCreate(); mServices.put(data.token, service); try { //【重要!!!】通知AMS, onCreate函数执行完成 ActivityManager.getService().serviceDoneExecuting( data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } catch (Exception e) { if (!mInstrumentation.onException(service, e)) { throw new RuntimeException( "Unable to create service " + data.info.name + ": " + e.toString(), e); } } }

来看看onCreate执行完了后,通知AMS,AMS的serviceDoneExecuting函数里做了什么呢?

【ActivityManagerService】

public void serviceDoneExecuting(IBinder token, int type, int startId, int res) { synchronized(this) { if (!(token instanceof ServiceRecord)) { Slog.e(TAG, "serviceDoneExecuting: Invalid service token=" + token); throw new IllegalArgumentException("Invalid service token"); } //调用ActiveServices的serviceDoneExecutingLocked函数 mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res); } }

【ActiveService.java】

void serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res) { boolean inDestroying = mDestroyingServices.contains(r); if (r != null) { if (type == ActivityThread.SERVICE_DONE_EXECUTING_START) { //不会执行 //startService的onStartCommand之后才会type为SERVICE_DONE_EXECUTING_START r.callStart = true; switch (res) { //根据onStartCommand的返回值做的不同处理 case Service.START_STICKY_COMPATIBILITY: case Service.START_STICKY: { ..... break; } case Service.START_NOT_STICK: { ...... break; } case Service.START_REDELIVER_INTENT: { ...... break; } case Service.START_TASK_REMOVED_COMPLETE: { ..... break; } default: throw new IllegalArgumentException( "Unknown service start result: " + res); } if (res == Service.START_STICKY_COMPATIBILITY) { r.callStart = false; } } else if (type == ActivityThread.SERVICE_DONE_EXECUTING_STOP) {//不会执行 ...... } final long origId = Binder.clearCallingIdentity(); //执行这个函数 serviceDoneExecutingLocked(r, inDestroying, inDestroying); Binder.restoreCallingIdentity(origId); } else { Slog.w(TAG, "Done executing unknown service from pid " + Binder.getCallingPid()); } }

再来看serviceDoneExecutingLocked函数

private void serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying, boolean finishing) { r.executeNesting--; if (r.executeNesting <= 0) { if (r.app != null) { r.app.execServicesFg = false; r.app.executingServices.remove(r); if (r.app.executingServices.size() == 0) { //移除之前在bumpxxx函数设置的超时消息 mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app); } else if (r.executeFg) { for (int i=r.app.executingServices.size()-1; i>=0; i--) { if (r.app.executingServices.valueAt(i).executeFg) { r.app.execServicesFg = true; break; } } } if (inDestroying) { mDestroyingServices.remove(r); r.bindings.clear(); } mAm.updateOomAdjLocked(r.app, true); } r.executeFg = false; if (r.tracker != null) { r.tracker.setExecuting(false, mAm.mProcessStats.getMemFactorLocked(), SystemClock.uptimeMillis()); if (finishing) { r.tracker.clearCurrentOwner(r, false); r.tracker = null; } } if (finishing) { if (r.app != null && !r.app.persistent) { r.app.services.remove(r); if (r.whitelistManager) { updateWhitelistManagerLocked(r.app); } } r.app = null; } } } 

requestServiceBingingsLocked

【ActiveServices.java】

 private final void requestServiceBindingsLocked(ServiceRecord r, boolean execInFg) throws TransactionTooLargeException { //其中r.bindings.size() = 1 for (int i=r.bindings.size()-1; i>=0; i--) { IntentBindRecord ibr = r.bindings.valueAt(i); if (!requestServiceBindingLocked(r, ibr, execInFg, false)) { break; } } }
private final boolean requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i, boolean execInFg, boolean rebind) throws TransactionTooLargeException { if (r.app == null || r.app.thread == null) { // If service is not currently running, can't yet bind. return false; } //其中i.requested = false,i.apps.size() = 1 if ((!i.requested || rebind) && i.apps.size() > 0) { try { //设置超时消息 bumpServiceExecutingLocked(r, execInFg, "bind"); r.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE); //【重要!!】异步调用ApplicationThread.scheduleBindService r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind, r.app.repProcState); if (!rebind) { i.requested = true; } i.hasBound = true; i.doRebind = false; } catch (TransactionTooLargeException e) { // Keep the executeNesting count accurate. if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Crashed while binding " + r, e); final boolean inDestroying = mDestroyingServices.contains(r); serviceDoneExecutingLocked(r, inDestroying, inDestroying); throw e; } catch (RemoteException e) { if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Crashed while binding " + r); // Keep the executeNesting count accurate. final boolean inDestroying = mDestroyingServices.contains(r); serviceDoneExecutingLocked(r, inDestroying, inDestroying); return false; } } return true; }

【ActivitytThread.java】

ActivityThread.ApplicationThread

 public final void scheduleBindService(IBinder token, Intent intent, boolean rebind, int processState) { updateProcessState(processState, false); BindServiceData s = new BindServiceData(); s.token = token; s.intent = intent; s.rebind = rebind; sendMessage(H.BIND_SERVICE, s); }

ActivityThread

 final H mH = new H(); private void sendMessage(int what, Object obj) { sendMessage(what, obj, 0, 0, false); } private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) { Message msg = Message.obtain(); msg.what = what; msg.obj = obj; msg.arg1 = arg1; msg.arg2 = arg2; if (async) { msg.setAsynchronous(true); } mH.sendMessage(msg); }

ActivityThread.H

public void handleMessage(Message msg) { switch (msg.what) { ... case BIND_SERVICE: Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind"); handleBindService((BindServiceData)msg.obj); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); break; ... } }

ActivityThread

private void handleBindService(BindServiceData data) { Service s = mServices.get(data.token); if (s != null) { try { data.intent.setExtrasClassLoader(s.getClassLoader()); data.intent.prepareToEnterProcess(); try { if (!data.rebind) { //调用自定义Service的onBind函数 IBinder binder = s.onBind(data.intent); //发布服务,即是onServiceConnected的流程 ActivityManager.getService().publishService( data.token, data.intent, binder); } else { s.onRebind(data.intent); ActivityManager.getService().serviceDoneExecuting( data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0); } ensureJitEnabled(); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } catch (Exception e) { if (!mInstrumentation.onException(s, e)) { throw new RuntimeException( "Unable to bind to service " + s + " with " + data.intent + ": " + e.toString(), e); } } } }

接下来看publishService,这个函数就是调用onServiceConnected的过程。

publishService

【ActivityManagerService.java】

 public void publishService(IBinder token, Intent intent, IBinder service) { if (intent != null && intent.hasFileDescriptors() == true) { throw new IllegalArgumentException("File descriptors passed in Intent"); } synchronized(this) { if (!(token instanceof ServiceRecord)) { throw new IllegalArgumentException("Invalid service token"); } //调用ActiveServices的publishServiceLocked mServices.publishServiceLocked((ServiceRecord)token, intent, service); } }

【ActiveServices.java】

void publishServiceLocked(ServiceRecord r, Intent intent, IBinder service) { final long origId = Binder.clearCallingIdentity(); try { if (r != null) { Intent.FilterComparison filter = new Intent.FilterComparison(intent); IntentBindRecord b = r.bindings.get(filter); if (b != null && !b.received) { b.binder = service; b.requested = true; b.received = true; for (int conni=r.connections.size()-1; conni>=0; conni--) { ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni); for (int i=0; i<clist.size(); i++) { ConnectionRecord c = clist.get(i); if (!filter.equals(c.binding.intent.intent)) { continue; } try { //【重要!!】异步调用LoadedApk.ServiceDispatcher.InnerConnection.connected c.conn.connected(r.name, service, false); } catch (Exception e) { Slog.w(TAG, "Failure sending service " + r.name + " to connection " + c.conn.asBinder() + " (in " + c.binding.client.processName + ")", e); } } } } //处理完后,调用serviceDoneExecutingLocked移除之前设置的超时消息 serviceDoneExecutingLocked(r, mDestroyingServices.contains(r), false); } } finally { Binder.restoreCallingIdentity(origId); } } 

接下来看看connected函数

【LoadedApk.java】

LoadedApk.ServiceDispatcher.InnerConnection

 private static class InnerConnection extends IServiceConnection.Stub { //避免内存泄漏,使用弱引用 final WeakReference<LoadedApk.ServiceDispatcher> mDispatcher; InnerConnection(LoadedApk.ServiceDispatcher sd) { mDispatcher = new WeakReference<LoadedApk.ServiceDispatcher>(sd); } public void connected(ComponentName name, IBinder service, boolean dead) throws RemoteException { LoadedApk.ServiceDispatcher sd = mDispatcher.get(); if (sd != null) { //调用LoadedApk.ServiceDispatcher的connected函数 sd.connected(name, service, dead); } } }

LoadedApk.ServiceDispatcher

 public void connected(ComponentName name, IBinder service, boolean dead) { //其中mActivityThread = Handler (android.app.ActivityThread$H) {} if (mActivityThread != null) { mActivityThread.post(new RunConnection(name, service, 0, dead)); } else { doConnected(name, service, dead); } }

所以接下来会异步调用LoadedApk.ServiceDispatcher.RunConnection.run函数

LoadedApk.ServiceDispatcher.RunConnection

 public void run() { //其中mCommand=0 if (mCommand == 0) { doConnected(mName, mService, mDead); } else if (mCommand == 1) { doDeath(mName, mService); } }
//LoadedApk.ServiceDispatcher类中的成员函数 public void doConnected(ComponentName name, IBinder service, boolean dead) { ServiceDispatcher.ConnectionInfo old; ServiceDispatcher.ConnectionInfo info; synchronized (this) { if (mForgotten) { // We unbound before receiving the connection; ignore // any connection received. return; } old = mActiveConnections.get(name); if (old != null && old.binder == service) { // Huh, already have this one. Oh well! return; } if (service != null) { // A new service is being connected... set it all up. info = new ConnectionInfo(); info.binder = service; info.deathMonitor = new DeathMonitor(name, service); try { service.linkToDeath(info.deathMonitor, 0); mActiveConnections.put(name, info); } catch (RemoteException e) { // This service was dead before we got it... just // don't do anything with it. mActiveConnections.remove(name); return; } } else { // The named service is being disconnected... clean up. mActiveConnections.remove(name); } if (old != null) { old.binder.unlinkToDeath(old.deathMonitor, 0); } } //其中old=null,dead=false, //service=com.example.testdymamicreceiver.TestService$ServiceBinder@fc085f0 if (old != null) { mConnection.onServiceDisconnected(name); } if (dead) { mConnection.onBindingDied(name); } if (service != null) { //调用bindService时传入的ServiceConnection的onServiceConnected函数 mConnection.onServiceConnected(name, service); } } 

到这里,bindService的流程才是走完了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231553.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android中实现二级菜单

    Android中实现二级菜单公司项目里的需求,商城的左下角,要求有分类的Menu。看图:1.当点击一级分类,修改二级菜单里面的内容。2.同时一级菜单里的背景变为右边有白色三角的背景。3.当点击二级菜单的条目时候,Popupwindow消失,并且界面更新对应条目的商品数据。我的实现思路是:左下角弹出Popupwindow,里面使用两个ListView实现。我的Demo的效果:

    2022年5月9日
    41
  • idea代码颜色搭配方案_idea设置注释颜色

    idea代码颜色搭配方案_idea设置注释颜色Github开发的Atom编辑器相信大家都已经用过了。经过这两年的迭代开发,内存占用,启动慢,卡顿很多大坑都已经填上。目前他已经是我写shell/bat/js的主力,当前除了插件安装比较麻烦(需要梯子,或者github+npminstall)我最满意的一点就是Atom的配色方案。他的OneDark配色方案是我见过的最赞的方案之一。深沉的背景/天蓝色的函数/浅灰色的注释/粉色关键字/红色的变量/

    2022年8月28日
    8
  • 前端如何做好seo_seo的五个步骤

    前端如何做好seo_seo的五个步骤一、合理的title、description、keywords搜索对着三项的权重逐个减小,title值强调重点即可;description把页面内容高度概括,不可过分堆砌关键词;keywords列举出重要关键词。1、titletitle,就是浏览器上显示的那些内容,不仅用户能看到,也能被搜索引擎检索到(搜索引擎在抓取网页时,最先读取的就是网页标题,所以title是否正确设置极其…

    2022年9月18日
    2
  • 按位异或运算符^

    按位异或运算符^参与运算的两个值,如果两个相应位相同,则结果为0,否则为1。即:0^0=0,1^0=1,0^1=1,1^1=0例如:10100001^00010001=101100000^0=0,0^1=10异或任何数=任何数1^0=1,1^1=01异或任何数-任何数取反任何数异或自己=把自己置0(1)按位异或可以用来使某些特定的位翻转,如对数10100001的第2位和第3位翻转,可以将数与000

    2022年6月5日
    32
  • jquery获取第几个子元素_js获取元素的指定子元素

    jquery获取第几个子元素_js获取元素的指定子元素jquery如何获取第一个或最后一个子元素?

    2022年4月20日
    62
  • python numba_如何用numba加速python?

    python numba_如何用numba加速python?我把写好的 markdown 导入进来 但是没想到知乎的排版如此感人 如果对知乎排版不满想要看高清清爽版 请移步微信公众号原文如何用 numba 加速 python 同时欢迎关注前言说道现在最流行的语言 就不得不提 python 可是 python 虽然容易上手 但速度却有点感人 如何用简单的方法让 python 加速到近乎可以媲美 C 的速度呢 今天来就来谈谈 numba 这个宝贝 对你没看错 不是 numpy 就是 num

    2025年6月11日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号