bindService流程
bindService(i, object : ServiceConnection {override fun onServiceDisconnected(p0: ComponentName?) {}override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {var msg = Message()msg.arg1 = 119msg.replyTo = Messenger(handler)Messenger(p1).send(msg)}}, Service.BIND_AUTO_CREATE)
ContextWrapper@Overridepublic boolean bindService(Intent service, ServiceConnection conn,int flags) {return mBase.bindService(service, conn, flags);}
ContextImpl.java@Overridepublic boolean bindService(Intent service, ServiceConnection conn, int flags) {warnIfCallingFromSystemProcess();return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), null,getUser());}private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,String instanceName, Handler handler, Executor executor, UserHandle user) {// Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser.IServiceConnection sd;if (conn == null) {throw new IllegalArgumentException("connection is null");}if (handler != null && executor != null) {throw new IllegalArgumentException("Handler and Executor both supplied");}if (mPackageInfo != null) {if (executor != null) {sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), executor, flags);} else {sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);}} 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);//AMS 第一次跨进程 IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);int res = ActivityManager.getService().bindIsolatedService(mMainThread.getApplicationThread(), getActivityToken(), service,service.resolveTypeIfNeeded(getContentResolver()),sd, flags, instanceName, 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.javapublic int bindIsolatedService(IApplicationThread caller, IBinder token, Intent service,String resolvedType, IServiceConnection connection, int flags, String instanceName,String callingPackage, int userId) throws TransactionTooLargeException {synchronized(this) {return mServices.bindServiceLocked(caller, token, service,resolvedType, connection, flags, instanceName, callingPackage, userId);}}
ActiveServicesint bindServiceLocked(IApplicationThread caller, IBinder token, Intent service,String resolvedType, final IServiceConnection connection, int flags,String instanceName, String callingPackage, final int userId)throws TransactionTooLargeException {bringUpServiceLocked(serviceRecord,serviceIntent.getFlags(),callerFg, false, false);//final IServiceConnection conn; // The client connection.c.conn.connected(s.name, b.intent.binder, false);}private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg,boolean whileRestarting, boolean permissionsReviewRequired)throws TransactionTooLargeException {realStartServiceLocked(r, app, execInFg);}private final void realStartServiceLocked(ServiceRecord r,ProcessRecord app, boolean execInFg) throws RemoteException {app.thread.scheduleCreateService(r, r.serviceInfo,mAm.compatibilityInfoForPackage(r.serviceInfo.applicationInfo),app.getReportedProcState());requestServiceBindingsLocked(r, execInFg);}private final boolean requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i,boolean execInFg, boolean rebind) throws TransactionTooLargeException {r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind,r.app.getReportedProcState());
}ApplicationThread
sendMessage(H.BIND_SERVICE, s);ActivityThreadcase BIND_SERVICE:Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");handleBindService((BindServiceData)msg.obj);Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);break;handleBindService{IBinder binder = s.onBind(data.intent);ActivityManager.getService().publishService(data.token, data.intent, binder);
}ActivityManagerService.java
mServices.publishServiceLocked((ServiceRecord)token, intent, service);ActiveServices.java
c.conn.connected(r.name, service, false);
bindService流程相关推荐
- bindService流程详解
bindService的流程,入口同样在ContextImpl中. ContextImpl.java public boolean bindService(Intent service, Servic ...
- Binder相关面试总结(七):AIDL内部的实现原理是什么
AIDL的作用是实现跨进程通讯使用方法也非常的简单,他的设计模式是典型的C/S架构.使用AIDL只要在Client端和Server端的项目根目录下面创建一个aidl的文件夹,在aidl文件夹的下面用j ...
- 深入理解Binder机制4-bindService过程分析
一.概述 1.1 Binder架构 Android内核基于Linux系统,而Linux系统进程间通信方式有很多,如管道,共g享内存,信号,信号量,消息队列,套接字.而Android为什么要用binde ...
- 【近3万字分享】《Android开发之路——10年老开发精心整理分享》
目录 前言 1 Android开发学习路线 1.1 大神最新总结(推荐直接看这个) 2021 最新Android知识体系 1.2按内容划分 1.3按阶段划分 1.4Android进阶路线(思维导图) ...
- 由浅入深 学习 Android Binder(十一) binder线程池
Android Binder系列文章: 由浅入深 学习 Android Binder(一)- AIDL 由浅入深 学习 Android Binder(二)- bindService流程 由浅入深 学习 ...
- 程序员吐槽_程序员在地铁上写代码被路人吐槽:有什么好装的!网友评论炸锅了!...
现在的人自我意识越来越强,专注做自己的事不太会在意别人的眼光,不管别人如何评价路,毕竟脚下的路是自己在走. 最近有位网友在职场论坛上发帖,说看见地铁上有一程序员大哥在写代码,并且配上了一张照片. 从照 ...
- 由浅入深 学习 Android Binder(一)- AIDL
Android Binder系列文章: 由浅入深 学习 Android Binder(一)- AIDL 由浅入深 学习 Android Binder(二)- bindService流程 由浅入深 学习 ...
- Android Binder机制总结
知识要点: 1. Binder通信机制的架构 2. 实现进程间通信 的步骤 3. 完整的Binder通信过程 4. Binder驱动的内核 空间和用户空间的数据传输 5. AIDL工具的原理 6. B ...
- 今年比亚迪招人招疯了......
背景 都说比亚迪今年疯狂招人,许多校招生都很顺利地收到了比亚迪的offer,究竟有多顺利呢?来看看这位网友的经历: 下午一点投简历,一点十五分打电话来面试,简单了解一下之后就说面试通过,等hr来谈薪, ...
最新文章
- 干货!隐马尔科夫模型
- 拿着锤子找钉子,数字芯片领导者比特大陆进军人工智能
- 计算机科学技术的教育应用论文,浅谈计算机科学技术在计算机教学中的应用论文...
- 【商业】10个经济学概念,让你看清复杂世界
- 推荐系统知识梳理——协同过滤
- 初识java——Java程序的历史、开发和运行
- opencv中的Mat图使用CDC显示
- 在linux下运行锐捷客户端,锐捷Linux客户端使用方法(基于ubuntu16.04)
- 电影数据集TMDB数据分析练习
- 数值计算之 梯度向量和梯度矩阵,雅可比矩阵,海森矩阵
- 外盘琪貨正大国际:别放弃勤奋,再难,别丢掉善良
- 跑步c语言,小程序 跑步 记录跑步代码 记时间和位置更新
- ATO(韩商爱拓)1Gbit SPI NAND Flash领先全球
- 第9章 Linux文件核心属性知识
- 线上宠物销售系统的设计与实现
- excel vba 修改单元格的颜色
- GStreamer播放教程05——色彩平衡
- matlab进行数值积分的主要函数使用方法
- 2021-09-03 【N02】小程序开发学习笔记
- 华为设备配置IS-IS命令