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. python学习笔记03:python的核心数据类型

    从根本上讲,Python是一种面向对象的语言.它的类模块支持多态,操作符重载和多重继承等高级概念,并且以Python特有的简洁的语法和类型,OOP十分易于使用.Python的语法简单,容易上手. Py ...

  2. iOS 出现错误reason: image not found的解决方案

    在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...

  3. TCP系列33—窗口管理&流控—7、Silly Window Syndrome(SWS)

    一.SWS介绍 前面我们已经通过示例看到如果接收端的应用层一直没有读取数据,那么window size就会慢慢变小最终可能变为0,此时我们假设一种场景,如果应用层读取少量数据(比如十几bytes),接 ...

  4. YaoLingJump开发者日志(二)

      熟悉了一点LGame里的套路,可以正式开工了.   增加了一个信息栏,显示得分.硬币数.生命值和当前关卡(仿照了超级玛丽的布局).   准备瑶玲的各种动画(静止.奔跑.跳跃.趴下.休息和死亡等). ...

  5. 数字证书认证这点事, SSL/TLS,OpenSSL

    1.概念 数字证书 HTTPS请求时,Server发给浏览器的认证数据,用私钥签名,并且告诉浏览器公钥,利用公钥解密签名,确认Server身份. 证书还会指明相应的CA,CA能确认证书是否真的是CA颁 ...

  6. Java线程模型

    并发不一定要依赖多线程(如PHP中很常见的多进程并发),但是在Java里面谈论并发,大多数都与线程脱不开关系. 线程是比进程更轻量级的调度执行单位,线程的引入,可以把一个进程的资源分配和执行调度分开, ...

  7. SPD各模块总结

    一.用户角色绑定节点 1.库存操作员.库存主管.验货操作员:绑定任一节点 2.采购操作员.公药操作员:只能绑定药库节点 3.退库操作员.药品申领员:绑定药库以外的节点 二.采购计划模块 1.采购计划的 ...

  8. nargchk函数 matlab【转】

    功能说明 验证输入参数的个数   函数语法 msgstring = nargchk(minargs, maxargs, numargs)msgstring = nargchk(minargs, max ...

  9. Flask的第一个应用

    Flask 是一个 Python 实现的 Web 开发微框架,微框架中的“微”意味着 Flask 旨在保持核心简单而易于扩展. 与Django功能上比较: Django:中间件,路由系统,视图(CBV ...

  10. BZOJ3427 Poi2013 Bytecomputer 【dp】

    题目链接 BZOJ3427 题解 容易发现最终序列一定是\(\{-1,0,1\}\)组成的 因为如果有一个位置不是,那么这个位置一定大于\(1\),那么上一个位置一定为\(1\),所以该位置一定加到过 ...