Activity与Service的回收
Android开发中,一个Application,运行在一个进程中。这个Application的各种组件(四种组件),通常是运行在同一个进程中的。但是,并不是绝对的。由于某种需求,比如,你可以设置AppA的组件Activity_A,运行在另外一个进程(ProcessB),通过设置Activity_A的属性android:process来实现。
这是关于进程和组件方面的关系。这些信息,与接下来的资源回收有关。当系统内存不足的时候,系统会按照进程的优先级来kill部分进程,优先级越低的进程,优先被Kill掉,而当某个进程被kill掉时,那么,在该进程中运行的组件,也会被销毁掉。
而进程的优先级,又是由运行在该进程中的组件的状态所决定。
进程的优先级,类型定义:
【
- 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:
- It hosts an
Activity
that the user is interacting with (theActivity
'sonResume()
method has been called). - It hosts a
Service
that's bound to the activity that the user is interacting with. - It hosts a
Service
that's running "in the foreground"—the service has calledstartForeground()
. - It hosts a
Service
that's executing one of its lifecycle callbacks (onCreate()
,onStart()
, oronDestroy()
). - It hosts a
BroadcastReceiver
that's executing itsonReceive()
method.
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.
- It hosts an
- 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 (itsonPause()
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.
- It hosts an
- 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. - 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. - 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的回收的更多相关文章
- Android Activity、Service、BroadcastReceiver 的生命周期
Activity.Service.BroadcastReceiver这三个组建是Android开发中最常使用到的组件,在它们的生命周期的各个阶段我们需要针对性的做些事情,了解这些组件的生命周期有利于我 ...
- Android开机启动Activity或者Service方法
本文出自 “Bill_Hoo专栏” 博客,请务必保留此出处http://billhoo.blog.51cto.com/2337751/761230 这段时间在做Android的基础开发,现在有一需求是 ...
- Activity与Service进行数据交互
Android启动Service有两种方法,一种是startService,一种是bindService.生命周期如下: 执行startService时,调用者如果没有stopService,Serv ...
- Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)
/*****************开启Activity 并传递参数*******************/ 使用am命令启动Activity并传递参数的方法,也能用作C层与Java进行数据传递的一 ...
- Activity和Service是否是在同一个进程中运行。
一般情况下,Activity和Service在同一个包名内,并且没有设定属性android:process=":remote",两者在同一个进程中. 因为一个进程只有一个UI线程, ...
- Android中Activity、Service和线程之间的通信
Activity.Service和线程应该是Android编程中最常见的几种类了,几乎大多数应用程序都会涉及到这几个类的编程,自然而然的,也就会涉及到三者之间的相互通信,本文就试图简单地介绍一下这三者 ...
- Activity与Service通信(不同进程之间)
使用Messenger 上面的方法只能在同一个进程里才能用,如果要与另外一个进程的Service进行通信,则可以用Messenger. 其实实现IPC(Inter-Process Communicat ...
- Activity和Service绑定
Activity和Service绑定后,可以方便Activity随时调用对应的Service里面的方法 绑定代码如下 Activity类代码: <span style="font-si ...
- Android Activity与Service的交互方式
参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...
随机推荐
- Thunder团队第二周 - Scrum会议7
Scrum会议7 小组名称:Thunder 项目名称:i阅app Scrum Master:杨梓瑞 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...
- java—连连看GUI
1.连连看棋盘图形化 package Link; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; impo ...
- 【IdentityServer4文档】- 支持和咨询选项
支持和咨询选项 我们为 IdentityServer 提供多个免费和商业支持及咨询选项. 免费支持 免费支持是基于社区的,而且使用的是公共论坛 StackOverflow 有越来越多的使用 Ident ...
- ALPHA-3
前言 失心疯病源3 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 16:00~20:18 援助行人模块并确定最终框架,顺便不死心的又找了一波车辆检测的dem ...
- error LNK2019: 无法解析的外部符号 该符号在函数 中被引用 解决方案
需要添加lib或者dll库.项目-属性-配置属性-链接器-输入-附件依赖项,添加需要的lib. 例如我在运行OSG程序的时候,忘记添加了附件依赖项就会报这个错. 解决方案如图.
- python获取指定长度的字符串
from random import Random def random_str(randomlength=31): str = '' chars = 'abcdefghijklmnopqrstuvw ...
- 【week2】 构建之法 读后感及问题
上一次读后感涵盖前五章的内容包括个人技术,结对合作,小组项目等.本周作业的燃尽图以及站立会议是关于<构建之法>第六章的内容,所以关于这一章的读后感涵盖在上两篇博客中. 第七章 MSF 介绍 ...
- 微信小程序 功能函数 计时器
let lovetime = setInterval(function () { let str = '(' + n + ')' + '重新获取' that.setData({ getText2: s ...
- Android基础------通知栏
前言:Android通知栏提示笔记 通知几乎是每一款app都拥有的功能 1.发送通知 发送一个通知栏必须用到两个类: NotificationManager . Notification. Noti ...
- hdu 3367 Pseudoforest (最小生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...