Android中Service在新进程中的启动流程

       

目录

1、Service与AMS交互框架介绍

1.1、认识AMS代表IActivityManager

1.2、认识客户端代表IApplicationThread

2、Service启动流程概览


        我们知道Android有四大组件,Activity、Service、ContentProvider、Broadcast,每个组件在系统运行中或者我们编写应用程序的过程中都起到举足轻重的作用,其中的Service主要用于做一些后台计算的问题,该篇文章介绍Service的启动流程概览,让你更加对Service有深入的理解,分析源码基于Android2.3.7。

1、Service与AMS交互框架介绍

        App中的Service与AMS的交互框架如下:

        这里涉及到两个进程,三个主体:

  • 首先是我们的App进程,假定MyService服务运行于服务进程(还没有启动,我们暂称为Service进程吧,从APP进程中调用startService启动)中 ,我们调用startService启动服务后,AMS会通过binder驱动回调到我们的Service进程,调用Service里面的生命周期函数。
  • 其次是AMS服务,该服务运行于system_server进程,用于接收并处理APP进程发送过来的请求,比如我们这里的startService,处理完成后回调到相应的客户端进程,如MyService进程。

        MyService服务由客户端进程实现,我们假设它在一个新进程中启动,它在与AMS通信时是通过远程接口IActivityManager与AMS通信的,这里的IActivityManager接口之所以说是远程接口是因为它的实现在AMS服务这一侧,而该服务运行于系统进程system_server中,而MyService则是运行于MyService进程,它们并不是在同一个进程。

        MyService进程通过代理ActivityManagerProxy与AMS交互,而它们之间通信的基础则是binder驱动。

ActivityManagerProxy实现了IActivityManager接口,客户端实际上是用ActivityManagerProxy与AMS通信。

1.1、认识AMS代表IActivityManager

        下面我们来认识下IActivityManager,看看它有哪些与服务相关的接口,源码如下:

/*** System private API for talking with the activity manager service.  This* provides calls from the application back to the activity manager.** {@hide}*/
public interface IActivityManager extends IInterface {//...public ComponentName startService(IApplicationThread caller, Intent service,String resolvedType) throws RemoteException;public int stopService(IApplicationThread caller, Intent service,String resolvedType) throws RemoteException;public boolean stopServiceToken(ComponentName className, IBinder token,int startId) throws RemoteException;public void setServiceForeground(ComponentName className, IBinder token,int id, Notification notification, boolean keepNotification) throws RemoteException;public int bindService(IApplicationThread caller, IBinder token,Intent service, String resolvedType,IServiceConnection connection, int flags) throws RemoteException;public boolean unbindService(IServiceConnection connection) throws RemoteException;public void publishService(IBinder token,Intent intent, IBinder service) throws RemoteException;public void unbindFinished(IBinder token, Intent service,boolean doRebind) throws RemoteException;/* oneway */public void serviceDoneExecuting(IBinder token, int type, int startId,int res) throws RemoteException;public IBinder peekService(Intent service, String resolvedType) throws RemoteException;//....}

        该文件接口比较多,这里只摘取了与Service相关的一些接口:

  • startService:启动服务(该接口的实现位于AMS侧)
  • stopService:停止服务(该接口的实现位于AMS侧)
  • bindService:绑定服务(该接口的实现位于AMS侧)
  • unbindService:解绑服务(该接口的实现位于AMS侧)

        以上接口由AMS实现;AMS处理完客户端的请求后,需要告诉MyService进程,并让MyService进程执行生命周期,此时AMS则通过IApplicationThread与MyService进程通信,IApplicationThread其实就是MyService进程的代表。

1.2、认识客户端代表IApplicationThread

        下面我们认识下IApplicationThread,部分源码如下:

/*** System private API for communicating with the application.  This is given to* the activity manager by an application  when it starts up, for the activity* manager to tell the application about things it needs to do.** {@hide}*/
public interface IApplicationThread extends IInterface {//...void schedulePauseActivity(IBinder token, boolean finished, boolean userLeaving,int configChanges) throws RemoteException;void scheduleStopActivity(IBinder token, boolean showWindow,int configChanges) throws RemoteException;void scheduleWindowVisibility(IBinder token, boolean showWindow) throws RemoteException;void scheduleResumeActivity(IBinder token, boolean isForward) throws RemoteException;void scheduleSendResult(IBinder token, List<ResultInfo> results) throws RemoteException;void scheduleLaunchActivity(Intent intent, IBinder token, int ident,ActivityInfo info, Bundle state, List<ResultInfo> pendingResults,List<Intent> pendingNewIntents, boolean notResumed, boolean isForward)throws RemoteException;void scheduleRelaunchActivity(IBinder token, List<ResultInfo> pendingResults,List<Intent> pendingNewIntents, int configChanges,boolean notResumed, Configuration config) throws RemoteException;void scheduleNewIntent(List<Intent> intent, IBinder token) throws RemoteException;void scheduleDestroyActivity(IBinder token, boolean finished,int configChanges) throws RemoteException;void scheduleReceiver(Intent intent, ActivityInfo info, int resultCode,String data, Bundle extras, boolean sync) throws RemoteException;static final int BACKUP_MODE_INCREMENTAL = 0;static final int BACKUP_MODE_FULL = 1;static final int BACKUP_MODE_RESTORE = 2;void scheduleCreateBackupAgent(ApplicationInfo app, int backupMode) throws RemoteException;void scheduleDestroyBackupAgent(ApplicationInfo app) throws RemoteException;void scheduleCreateService(IBinder token, ServiceInfo info) throws RemoteException;void scheduleBindService(IBinder token,Intent intent, boolean rebind) throws RemoteException;void scheduleUnbindService(IBinder token,Intent intent) throws RemoteException;void scheduleServiceArgs(IBinder token, int startId, int flags, Intent args)throws RemoteException;void scheduleStopService(IBinder token) throws RemoteException;static final int DEBUG_OFF = 0;static final int DEBUG_ON = 1;static final int DEBUG_WAIT = 2;void bindApplication(String packageName, ApplicationInfo info, List<ProviderInfo> providers,ComponentName testName, String profileName, Bundle testArguments, IInstrumentationWatcher testWatcher, int debugMode, boolean restrictedBackupMode,Configuration config, Map<String, IBinder> services) throws RemoteException;void scheduleExit() throws RemoteException;void scheduleSuicide() throws RemoteException;void requestThumbnail(IBinder token) throws RemoteException;void scheduleConfigurationChanged(Configuration config) throws RemoteException;void updateTimeZone() throws RemoteException;void processInBackground() throws RemoteException;void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args)throws RemoteException;void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,int resultCode, String data, Bundle extras, boolean ordered, boolean sticky)throws RemoteException;void scheduleLowMemory() throws RemoteException;void scheduleActivityConfigurationChanged(IBinder token) throws RemoteException;void profilerControl(boolean start, String path, ParcelFileDescriptor fd)throws RemoteException;void setSchedulingGroup(int group) throws RemoteException;void getMemoryInfo(Debug.MemoryInfo outInfo) throws RemoteException;static final int PACKAGE_REMOVED = 0;static final int EXTERNAL_STORAGE_UNAVAILABLE = 1;void dispatchPackageBroadcast(int cmd, String[] packages) throws RemoteException;void scheduleCrash(String msg) throws RemoteException;String descriptor = "android.app.IApplicationThread";//...
}

        与Service相关的接口如下:

  • scheduleCreateService:创建服务,会调用服务的onCreate生命周期函数。
  • scheduleBindService:绑定服务,会调用服务的onBind函数。
  • scheduleUnbindService:解绑服务,会调用服务的unBind函数。
  • scheduleServiceArgs:调用服务的onStart函数。
  • scheduleStopService:停止服务,调用服务的onStop函数。

        以上的函数运行于MyService进程,由客户端实现。

2、Service启动流程概览

        如下图所示为Service启动的总体流程:

        如上面的流程图,Service启动总共分为以上8个步骤,接下来我们就按照这个8个步骤分析下Service是如何通过调用startService一步步启动的,在执行每一步的过程中希望读者清楚当前每一步是在哪个进程执行,这样才能真正理解为何启动Service时与AMS的具体交互是怎样的,同时有助于理解启动服务过程中为何会产生ANR。

        好了,启动流程概览基本说完了;下一篇我们在具体解析startService的启动流程。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/68364.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【信息系统项目管理师-选择真题】2011下半年综合知识答案和详解

更多内容请见: 备考信息系统项目管理师-专栏介绍和目录 文章目录 【第1题】【第2题】【第3题】【第4题】【第5题】【第6题】【第7题】【第8题】【第9~10题】【第11题】【第12题】【第13题】【第14题】【第15题】【第16题】【第17题】【第18题】【第19题】【第20题】【第21题】…

read+write实现:链表放到文件+文件数据放到链表 的功能

思路 一、 定义链表&#xff1a; 1 节点结构&#xff08;数据int型&#xff09; 2 链表操作&#xff08;创建节点、插入节点、释放链表、打印链表&#xff09;。 二、链表保存到文件 1打开文件 2遍历链表、写文件&#xff1a; 遍历链表,write()将节点数据写入文件。…

【景区导游——LCA】

题目 代码 #include <bits/stdc.h> using namespace std; using ll long long; const int N 1e5 10; const int M 2 * N; int p[N][18], d[N], a[N]; ll dis[N][18]; //注意这里要开long long int h[N], e[M], ne[M], idx, w[M]; int n, k; void add(int a, int b, …

Vue 3 30天精进之旅:Day 06 - 表单输入绑定

引言 在前几天的学习中&#xff0c;我们探讨了事件处理的基本概念及其在Vue中的应用。今天&#xff0c;我们将进一步了解Vue的表单输入绑定。这是构建用户交互式应用的核心部分&#xff0c;使得我们能够方便地处理用户输入并实时更新数据。本文将介绍如何在Vue中实现单向和双向…

二进制安卓清单 binary AndroidManifest - XCTF apk 逆向-2

XCTF 的 apk 逆向-2 题目 wp&#xff0c;这是一道反编译对抗题。 题目背景 AndroidManifest.xml 在开发时是文本 xml&#xff0c;在编译时会被 aapt 编译打包成为 binary xml。具体的格式可以参考稀土掘金 MindMac 做的类图&#xff08;2014&#xff09;&#xff0c;下面的博…

反向代理模块。。

1 概念 1.1 反向代理概念 反向代理是指以代理服务器来接收客户端的请求&#xff0c;然后将请求转发给内部网络上的服务器&#xff0c;将从服务器上得到的结果返回给客户端&#xff0c;此时代理服务器对外表现为一个反向代理服务器。 对于客户端来说&#xff0c;反向代理就相当于…

AI常见的算法

人工智能&#xff08;AI&#xff09;中常见的算法分为多个领域&#xff0c;如机器学习、深度学习、强化学习、自然语言处理和计算机视觉等。以下是一些常见的算法及其用途&#xff1a; 1. 机器学习 (Machine Learning) 监督学习 (Supervised Learning) 线性回归 (Linear Regr…

flink StreamGraph解析

Flink程序有三部分operation组成&#xff0c;分别是源source、转换transformation、目的地sink。这三部分构成DAG。 DAG首先生成的是StreamGraph。 用户代码在添加operation的时候会在env中缓存&#xff08;变量transformations&#xff09;&#xff0c;在env.execute()执行的…

WPS数据分析000010

基于数据透视表的内容 一、排序 手动调动 二、筛选 三、值显示方式 四、值汇总依据 五、布局和选项 不显示分类汇总 合并居中带标签的单元格 空单元格显示 六、显示报表筛选页

【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】1.18 逻辑运算引擎:数组条件判断的智能法则

1.18 逻辑运算引擎&#xff1a;数组条件判断的智能法则 1.18.1 目录 #mermaid-svg-QAFjJvNdJ5P4IVbV {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-QAFjJvNdJ5P4IVbV .error-icon{fill:#552222;}#mermaid-svg-QAF…

Tensor 基本操作2 理解 tensor.max 操作,沿着给定的 dim 是什么意思 | PyTorch 深度学习实战

前一篇文章&#xff0c;Tensor 基本操作1 | PyTorch 深度学习实战 本系列文章 GitHub Repo: https://github.com/hailiang-wang/pytorch-get-started 目录 Tensor 基本操作torch.max默认指定维度 Tensor 基本操作 torch.max torch.max 实现降维运算&#xff0c;基于指定的 d…

【ESP32】ESP-IDF开发 | WiFi开发 | UDP用户数据报协议 + UDP客户端和服务器例程

1. 简介 UDP协议&#xff08;User Datagram Protocol&#xff09;&#xff0c;全称用户数据报协议&#xff0c;它是一种面向非连接的协议&#xff0c;面向非连接指的是在正式通信前不必与对方先建立连接&#xff0c; 不管对方状态就直接发送。至于对方是否可以接收到这些数据内…

动手学深度学习-卷积神经网络-3填充和步幅

目录 填充 步幅 小结 在上一节的例子&#xff08;下图&#xff09; 中&#xff0c;输入的高度和宽度都为3&#xff0c;卷积核的高度和宽度都为2&#xff0c;生成的输出表征的维数为22。 正如我们在 上一节中所概括的那样&#xff0c;假设输入形状为nhnw&#xff0c;卷积核形…

Airflow:精通Airflow任务依赖

任务依赖关系是任何工作流管理系统的核心概念&#xff0c;Apache Airflow也不例外。它们确定在工作流中执行任务的顺序和条件&#xff0c;确保以正确的顺序完成任务&#xff0c;并确保在相关任务开始之前成功完成先决任务。在本文中我们将探讨Apache Airflow中的任务依赖关系&a…

【数据结构】_链表经典算法OJ:合并两个有序数组

目录 1. 题目描述及链接 2. 解题思路 3. 程序 3.1 第一版 3.2 第二版 1. 题目描述及链接 题目链接&#xff1a;21. 合并两个有序链表 - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 将两个升序链表合并为一个新的 升序 链表并返回。 新链表是通过拼接给…

crontabl循环定时任务和at一次性任务深度使用

文章目录 crontabl【循环定时任务】crontabl说明参数说明格式说明使用示例使用实例脚本无法执行问题官方解决方法crontabl执行报错解决办法crontab中expect脚本不能正常运行解决方案定时任务执行sh脚本中含有的expect脚本方法给crontab添加环境变量 at【一次性定时任务】说明参…

ChatGPT高效处理图片技巧使用详解

ChatGPT&#xff0c;作为OpenAI开发的预训练语言模型&#xff0c;主要用于生成自然语言文本的任务。然而&#xff0c;通过一些技巧和策略&#xff0c;我们可以将ChatGPT与图像处理模型结合&#xff0c;实现一定程度上的图像优化和处理。本文将详细介绍如何使用ChatGPT高效处理图…

全程Kali linux---CTFshow misc入门

图片篇(基础操作) 第一题&#xff1a; ctfshow{22f1fb91fc4169f1c9411ce632a0ed8d} 第二题 解压完成后看到PNG&#xff0c;可以知道这是一张图片&#xff0c;使用mv命令或者直接右键重命名&#xff0c;修改扩展名为“PNG”即可得到flag。 ctfshow{6f66202f21ad22a2a19520cdd…

基于SMPL的三维人体重建-深度学习经典方法之VIBE

本文以开源项目VIBE[1-2]为例&#xff0c;介绍下采用深度学习和SMPL模板的从图片进行三维人体重建算法的整体流程。如有错误&#xff0c;欢迎评论指正。 一.算法流程 包含生成器模块和判别器模块&#xff0c;核心贡献就在于引入了GRU模块&#xff0c;使得当前帧包含了先前帧的先…