Android开发中,一个Application,运行在一个进程中。这个Application的各种组件(四种组件),通常是运行在同一个进程中的。但是,并不是绝对的。由于某种需求,比如,你可以设置AppA的组件Activity_A,运行在另外一个进程(ProcessB),通过设置Activity_A的属性android:process来实现。

这是关于进程和组件方面的关系。这些信息,与接下来的资源回收有关。当系统内存不足的时候,系统会按照进程的优先级来kill部分进程,优先级越低的进程,优先被Kill掉,而当某个进程被kill掉时,那么,在该进程中运行的组件,也会被销毁掉。

而进程的优先级,又是由运行在该进程中的组件的状态所决定。

进程的优先级,类型定义:

  1. Foreground process

    A process that is required for what the user is currently doing. A process is considered to be in the foreground if any of the following conditions are true:

    Generally, only a few foreground processes exist at any given time. They are killed only as a last resort—if memory is so low that they cannot all continue to run. Generally, at that point, the device has reached a memory paging state, so killing some foreground processes is required to keep the user interface responsive.

  2. Visible process

    A process that doesn't have any foreground components, but still can affect what the user sees on screen. A process is considered to be visible if either of the following conditions are true:

    • It hosts an Activity that is not in the foreground, but is still visible to the user (its onPause() method has been called). This might occur, for example, if the foreground activity started a dialog, which allows the previous activity to be seen behind it.
    • It hosts a Service that's bound to a visible (or foreground) activity.

    A visible process is considered extremely important and will not be killed unless doing so is required to keep all foreground processes running.

  3. Service process

    A process that is running a service that has been started with the startService() method and does not fall into either of the two higher categories. Although service processes are not directly tied to anything the user sees, they are generally doing things that the user cares about (such as playing music in the background or downloading data on the network), so the system keeps them running unless there's not enough memory to retain them along with all foreground and visible processes.

  4. Background process

    A process holding an activity that's not currently visible to the user (the activity's onStop() method has been called). These processes have no direct impact on the user experience, and the system can kill them at any time to reclaim memory for a foreground, visible, or service process. Usually there are many background processes running, so they are kept in an LRU (least recently used) list to ensure that the process with the activity that was most recently seen by the user is the last to be killed. If an activity implements its lifecycle methods correctly, and saves its current state, killing its process will not have a visible effect on the user experience, because when the user navigates back to the activity, the activity restores all of its visible state. See the Activities document for information about saving and restoring state.

  5. Empty process

    A process that doesn't hold any active application components. The only reason to keep this kind of process alive is for caching purposes, to improve startup time the next time a component needs to run in it. The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.

5种类型的进程。从中,可以得到与实际工作有关系的信息是:

1.Service是会被销毁的;它跟Activity一样,都是会被销毁掉的。所以,在系统调用onDestroy方法时,要保存数据,如果你想让操作过的数据持久化的话。

2.如果一个Service,你想提高它的优先级,不想让它所在的进程被提前kill掉,当系统资源不足时,那么,让该Service所在的进程类型是Forground process,是一种方法。那么,也就是说,在Service中调用startForground()方法。(Android系统在评定一个进程的优先级时,是尽量往高优先级评定的,比如,只要该进程中的组件,具备了Froground process定义的特征,那么,该进程就会被评定为Froground Process)

3.其它方面,上述信息,可以指导实际的工作。

数据持久化方面

既然,Activity,Service都是会销毁的,那么,就必然要考虑,在工作没有完成的情况下,如何保存已经完成的工作,也就是保存相关的数据。

对于Service,则是在onDestroy方法中,添加保存数据的逻辑。如,启动一个线程,将数据写到数据库中;或者,直接采用writeObject的方法,保存对象的数据;或者,将数据,写到文件系统中。

对于Service,如果,再次启动的时候,要恢复上次的工作情况,则是从保存的区域读取过去保存的数据,然后,再继续。

对于Activity,系统则提供了比较多回调方法如下图:

假定,Activity_A在AppA中运行;Activity_B在AppB中运行。

1.当当前屏幕中的Activity_A被其他Activity_B取代了,系统会回调被取代Activity的onSaveInstanceState()方法,在该方法中,保存用户实现了操作数据。

2.因为,Activity_A是不可见的,那么,相应它所运行的进程AppA也具备低优先级。当系统内存不足时,AppA就被系统kill掉。

3.用户再次启动AppA,因为,Activity_A实现了onSaveInstanceState()方法,该方法在Activity_A不可见时,系统会回调,也就是保存了Activity_A的状态。

那么,在重新创建Activity_A时,onCreate方法的的state参数,是不会为空的,系统已经为你保存了该State,在你实现onSaveInstanceState()方法时。

所以,系统对待Activity,比对待Service要好,帮Activity保存了一个状态;而Service,则没有。在Service中,用户要保存数据,是要自己写到数据库或者文件系统中去。

全文完。

引用资料:

http://developer.android.com/guide/components/processes-and-threads.html

http://developer.android.com/guide/components/services.html

http://developer.android.com/guide/components/activities.html

Activity与Service的回收的更多相关文章

  1. Android Activity、Service、BroadcastReceiver 的生命周期

    Activity.Service.BroadcastReceiver这三个组建是Android开发中最常使用到的组件,在它们的生命周期的各个阶段我们需要针对性的做些事情,了解这些组件的生命周期有利于我 ...

  2. Android开机启动Activity或者Service方法

    本文出自 “Bill_Hoo专栏” 博客,请务必保留此出处http://billhoo.blog.51cto.com/2337751/761230 这段时间在做Android的基础开发,现在有一需求是 ...

  3. Activity与Service进行数据交互

    Android启动Service有两种方法,一种是startService,一种是bindService.生命周期如下: 执行startService时,调用者如果没有stopService,Serv ...

  4. Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)

    /*****************开启Activity  并传递参数*******************/ 使用am命令启动Activity并传递参数的方法,也能用作C层与Java进行数据传递的一 ...

  5. Activity和Service是否是在同一个进程中运行。

    一般情况下,Activity和Service在同一个包名内,并且没有设定属性android:process=":remote",两者在同一个进程中. 因为一个进程只有一个UI线程, ...

  6. Android中Activity、Service和线程之间的通信

    Activity.Service和线程应该是Android编程中最常见的几种类了,几乎大多数应用程序都会涉及到这几个类的编程,自然而然的,也就会涉及到三者之间的相互通信,本文就试图简单地介绍一下这三者 ...

  7. Activity与Service通信(不同进程之间)

    使用Messenger 上面的方法只能在同一个进程里才能用,如果要与另外一个进程的Service进行通信,则可以用Messenger. 其实实现IPC(Inter-Process Communicat ...

  8. Activity和Service绑定

    Activity和Service绑定后,可以方便Activity随时调用对应的Service里面的方法 绑定代码如下 Activity类代码: <span style="font-si ...

  9. Android Activity与Service的交互方式

    参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...

随机推荐

  1. 2017秋-软件工程第四次作业(2)-结对使用TDD框架完成单元测试

    第一次接触“单元测试”这个要求,我和队友学习了一些示例后开始操作.如下展示一些建立单元测试的过程.Step1:右键单击[解决方案]->左键单击[添加(D)]->[新建项目(N)]. Ste ...

  2. iOS开发GCD的简单使用

    - (void)viewDidLoad { [super viewDidLoad]; // gcd 可以充分调用设备的 cpu 发挥最大性能,在 C 语言基础之上封装的 // dispatch_que ...

  3. Swift as as!和as?的区别

    1.as的使用场合 1.从派生类转换为基类,向上转类型(upcasting) class Animal{} class Dog:Animal{} let cat = ANimal() let dog ...

  4. TFS持续集成

    TFS持续集成的就是跟踪代码变更,合并,能够自定义脚本,任务进行自动化测试,发版,部署,有点像docker的味道.在这个代理服务器分布式中tfsserver起着能够随时拿去最新代码能够统一执行任务的角 ...

  5. 2-c语言作业1

    #include<stdio.h> #include<math.h> int main(void) { int money,year; double rate,sun; pri ...

  6. 按Backspace键删除时,会出现^H

    按Backspace键删除时,会出现^H 2014-08-12 19:38 1180人阅读 评论(0) 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 在linux/unix 平台的经常使 ...

  7. NeoLoad系列- 快速上手教程

    1.新建工程 2.点击录制脚本按钮 3.在弹出的开始录制对话框中,填写虚拟用户信息. Record in下拉框,用来填写用户路径,一般有三个容器组成: Init, Actions, and End.当 ...

  8. 显示系统中所有的socket信息

    netstat -aon /proc/net/tcp /proc/net/udp /proc/net/unix 相关的代码是:tcp4_seq_show(struct seq_file *file, ...

  9. WebExtensions & tabs.executeScript()

    tabs.executeScript() https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs ...

  10. 如何取得dbgrid中未保存(post)的值(50分)

    比如说处在编辑状态时,想取得当前记录值 Dataset.fields[0].Value 就是当前值:Dataset.fields[0].OldValue 就是原始值. 呵呵,我指得是在编辑时,就是按键 ...