欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android生命周期深入分析(二)

程序员文章站 2023-12-15 11:20:22
在 android 中,多数情况下每个程序都是在各自独立的 linux 进程中运行的。当一个程序或其某些部分被请求时,它的进程就“出生”了;当这个程序没有必要再运行下去且系...

在 android 中,多数情况下每个程序都是在各自独立的 linux 进程中运行的。当一个程序或其某些部分被请求时,它的进程就“出生”了;当这个程序没有必要再运行下去且系统需要回收这个进程的内存用于其他程序时,这个 进程就“死亡”了。可以看出,android 程序的生命周期是由系统控制而非程序自身直接控制。这和我们编写桌面应用程序时的思维有一些不同,一个桌面应用程序的进程也是在其他进程或用户请求时被创 建,但是往往是在程序自身收到关闭请求后执行一个特定的动作(比如从 main 函数中 return)而导致进程结束的。要想做好某种类型的程序或者某种平台下的程序的开发,最关键的就是要弄清楚这种类型的程序或整个平台下的程序的一般工作 模式并熟记在心。在 android 中,程序的生命周期控制就是属于这个范畴——我的个人理解:)

在 android 系统中,当某个 activity调用 startactivity(myintent) 时,系统会在所有已经安装的程序中寻找其 intent filter 和 myintent 最匹配的一个 activity,启动这个进程,并把这个 intent 通知给这个 activity。这就是一个程序的“生”。比如我们在 home application 中选择 “web browser”,系统会根据这个 intent 找到并启动 web browser 程序,显示 web browser 的一个 activity 供我们浏览网页(这个启动过程有点类似我们在在个人电脑上双击桌面上的一个图标,启动某个应用程序)。在 android 中,所有的应用程序“生来就是平等的”,所以不光 android 的核心程序甚至第三方程序也可以发出一个 intent 来启动另外一个程序中的一个 activity。android 的这种设计非常有利于“程序部件”的重用。

  一个 android 程序的进程是何时被系统结束的呢?通俗地说,一个即将被系统关闭的程序是系统在内存不足(low memory)时,根据“重要性层次”选出来的“牺牲品”。一个进程的重要性是根据其中运行的部件和部件的状态决定的。各种进程按照重要性从高到低排列如 下:
  1. 前台进程。这样的进程拥有一个在屏幕上显示并和用户交互的 activity 或者它的一个intentreciver 正在运行。这样的程序重要性最高,只有在系统内存非常低,万不得已时才会被结束。
  2. 可见进程。在屏幕上显示,但是不在前台的程序。比如一个前台进程以对话框的形式显示在该进程前面。这样的进程也很重要,它们只有在系统没有足够内存运行所有前台进程时,才会被结束。
  3. 服务进程。这样的进程在后台持续运行,比如后台音乐播放、后台数据上传下载等。这样的进程对用户来说一般很有用,所以只有当系统没有足够内存来维持所有的前台和可见进程时,才会被结束。
  4. 后台进程。这样的程序拥有一个用户不可见的 activity。这样的程序在系统内存不足时,按照 lru 的顺序被结束。
  5. 空进程。这样的进程不包含任何活动的程序部件。系统可能随时关闭这类进程。
 

从某种意义上讲,垃圾收集机制把程序员从“内存管理噩梦”中解放出来,而 android 的进程生命周期管理机制把用户从“任务管理噩梦”中解放出来。我见过一些 nokia s60 用户和 windows mobile 用户要么因为长期不关闭多余的应用程序而导致系统变慢,要么因为不时查看应用程序列表而影响使用体验。android 使用 java 作为应用程序 api,并且结合其独特的生命周期管理机制同时为开发者和使用者提供最大程度的便利。

activity lifecycle activity有三种基本状态
    active:处于屏幕前景(当前task的栈顶activity处于active状态),同一时刻只能有一个activity处于active状态; paused状态:处于背景画面画面状态,失去了焦点,但依然是活动状态; stopped:不可见,但依然保持所有的状态和内存信息。

可以调用finish()结束处理paused或者stopped状态的activity。

各种状态之间通过下列的函数调用转换

void oncreate(bundle savedinstancestate)
void onstart()
void onrestart()
void onresume()
void onpause()
void onstop()
void ondestroy()

activity的生命周期可以分为三组: the entire lifetime of an activity happens between the first call tooncreate() through to a single final call to ondestroy().

the visible lifetime of an activity happens between a call toonstart() until a corresponding call to onstop().

the foreground lifetime of an activity happens between a call to onresume() until a corresponding call toonpause().

android生命周期深入分析(二)

 

保存activity状态

to capture that state before the activity is killed, you can implement an onsaveinstancestate() method for the activity. android calls this method before making the activity vulnerable to being destroyed — that is, before onpause() is called. it passes the method a bundle object where you can record the dynamic state of the activity as name-value pairs. when the activity is again started, the bundle is passed both to oncreate() and to a method that's called after onstart(),onrestoreinstancestate(), so that either or both of them can recreate the captured state.

unlike onpause() and the other methods discussed earlier, onsaveinstancestate() and onrestoreinstancestate()are not lifecycle methods. they are not always called. because onsaveinstancestate() is not always called, you should use it only to record the transient state of the activity, not to store persistent data. use onpause() for that purpose instead. 启动另一个activity的过程 the current activity's onpause() method is called. next, the starting activity's oncreate(), onstart(), and onresume() methods are called in sequence. then, if the starting activity is no longer visible on screen, its onstop() method is called. service生命周期

a service can be used in two ways: it can be started and allowed to run until someone stops it or it stops itself. in this mode, it's started by callingcontext.startservice() and stopped by calling context.stopservice(). it can stop itself by callingservice.stopself() or service.stopselfresult(). only one stopservice() call is needed to stop the service, no matter how many times startservice() was called.

it can be operated programmatically using an interface that it defines and exports. clients establish a connection to the service object and use that connection to call into the service. the connection is established by callingcontext.bindservice(), and is closed by calling context.unbindservice(). multiple clients can bind to the same service. if the service has not already been launched, bindservice() can optionally launch it.

相关的方法:

void oncreate()
void onstart(intent intent)
void ondestroy()

the oncreate() and ondestroy() methods are called for all services, whether they're started bycontext.startservice() or context.bindservice(). however, onstart() is called only for services started bystartservice().

if a service permits others to bind to it, there are additional callback methods for it to implement:

ibinder onbind(intent intent)
boolean onunbind(intent intent)
void onrebind(intent intent)

android生命周期深入分析(二)

 

broadcast receiver lifecycle

只有一个方法:void onreceive(context curcontext, intent broadcastmsg)

a process with an active broadcast receiver is protected from being killed. but a process with only inactive components can be killed by the system at any time, when the memory it consumes is needed by other processes.

this presents a problem when the response to a broadcast message is time consuming and, therefore, something that should be done in a separate thread, away from the main thread where other components of the user interface run. ifonreceive() spawns the thread and then returns, the entire process, including the new thread, is judged to be inactive (unless other application components are active in the process), putting it in jeopardy of being killed. the solution to this problem is for onreceive() to start a service and let the service do the job, so the system knows that there is still active work being done in the process. 进程的生命周期

android根据其重要性在内存不足的时候移去重要性最低的进程。重要性由高到低为:

    前台进程 可见进程 服务进程 后台进程 空进程

注意:because a process running a service is ranked higher than one with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply spawn a thread — particularly if the operation will likely outlast the activity. 比如播放mp3的时候就要启动一个service。

上一篇:

下一篇: