最近看了Android 开发者文档中的线程和进程方面的内容,看后感觉Android的设计非常的巧妙与强大。将学习到的知识总结如下:

文档中的进程内容提到了进程优先级,我觉得很重要,对于我们理解Android的进程有很大的帮助:

原文附上:

ProcessesBy default, all components of the same application run in the same process and most applications should not change this. However, if you find that you need to control which process a certain component belongs to, you can do so in the manifest file.The manifest entry for each type of component element—<activity>, <service>, <receiver>, and <provider>—supports an android:process attribute that can specify a process in which that component should run. You can set this attribute so that each component runs in its own process or so that some components share a process while others do not. You can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.The <application> element also supports an android:process attribute, to set a default value that applies to all components.Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed. A process is started again for those components when there's again work for them to do.When deciding which processes to kill, the Android system weighs their relative importance to the user. For example, it more readily shuts down a process hosting activities that are no longer visible on screen, compared to a process hosting visible activities. The decision whether to terminate a process, therefore, depends on the state of the components running in that process. The rules used to decide which processes to terminate is discussed below.Process lifecycleThe Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes. To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" based on the components running in the process and the state of those components. Processes with the lowest importance are eliminated first, then those with the next lowest importance, and so on, as necessary to recover system resources.There are five levels in the importance hierarchy. The following list presents the different types of processes in order of importance (the first process is most important and is killed last):    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 (the Activity's onResume() 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 called startForeground().        It hosts a Service that's executing one of its lifecycle callbacks (onCreate(), onStart(), or onDestroy()).        It hosts a BroadcastReceiver that's executing its onReceive() 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.    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.    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.Android ranks a process at the highest level it can, based upon the importance of the components currently active in the process. For example, if a process hosts a service and a visible activity, the process is ranked as a visible process, not a service process.


文档已经说明了

进程优先级

文档的解释 Doc
                                           The manifest entry for each type of component element—<activity>, <service>, <receiver>, and <provider>—supports an android:process attribute that can specify a process in which that component should run. You can set this attribute so that each component runs in its own process or so that some components share a process while others do not. You can also set android:process so that components of different applications run in the same processprovided that the applications share the same Linux user ID and are signed with the same certificates                      
通过文档可以知道,可以通过Android的清单文件可以为android的组件 例如 activity、service、receiver指定组件运行的进程。通过设置 android:progress 设置此属性可以让不同的应用程序的组件运行在相同的进程中,多个应用程序共享相同的Linux的进程。
                                           Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed. A process is started again for those components when there's again work for them to do.                                                                  When deciding which processes to kill, the Android system weighs their relative importance to the user. For example, it more readily shuts down a process hosting activities that are no longer visible on screen, compared to a process hosting visible activities. The decision whether to terminate a process, therefore, depends on the state of the components running in that process. The rules used to decide which processes to terminate is discussed below.                       

Android能够决定关闭一个过程在某种程度上,当内存低,所需的其他进程,更直接的服务于用户。应用程序组件运行在这个过程中,也会因此被销毁。为这些组件重新开始一个过程时,再为他们工作。
当决定哪个进程的时候,Android系统向用户重他们的相对重要性。例如,它更容易关闭不在显示在屏幕上的activitys,而一个托管可见的activity progress 。因此,决定是否终止一个进程取决于组件中运行的状态的过程。终止进程的判定规则将是下面要讨论的。

Android中进程的生命周期: Android系统视图保持应用程序的进程,但是最终需要删除旧的进程来回收内存。Android系统把每个进程都按照“重要性层次结构”进行划分。android分出了5个重要性的层次水平。分别是:

进程优先级

  • 前台进程
    • 拥有一个正在与用户交互的activity(onResume调用)的进程
    • 拥有一个与正在和用户交互的activity绑定的服务的进程
    • 拥有一个正在“运行于前台”的服务——服务的startForeground方法调用
    • 拥有一个正在执行以下三个生命周期方法中任意一个的服务(onCreate(), onStart(), or onDestroy())
    • 拥有一个正在执行onReceive方法的广播接收者的进程
  • 可见进程

    • 拥有一个不在前台,但是对用户依然可见的activity(onPause方法调用)的进程
    • 拥有一个与可见(或前台)activity绑定的服务的进程
  • 服务进程

    • 以startService()开启的服务,就是一个服务进程,尽管服务过程不直接与用户看到的任何东西,他们通常做一些用户关心的(比如在后台播放音乐或下载数据网络上的)。
  • 后台进程

    • 当一个activity执行onstop方法被调用,或者当前的activity用户不可见,这些进程没有影响到用户的体验,并且系统可以在任何时间来回收他们。
  • 空进程
    • 不包含任何活动的应用程序组件。这种进程还能存在的唯一的原因就是用于缓存。

线程:
文档说明当应用程序启动时,系统为应用程序创建一个线程的执行,称为“主要。 “这个线程非常重要,因为它负责把事件分发给相应的用户界面小部件,包括屏幕绘图事件。 也是您的应用程序与组件交互的线程从Android UI toolkit(组件从Android。 小部件和android。 视图包)。 因此,主线程有时也称为UI线程。
系统不为每个组件的实例创建一个单独的线程。所有组件运行在相同的进程中实例化UI线程,每个组件和系统调用分派线程。因此,应对系统回调的方法(如onKeyDown()报告用户操作或生命周期回调方法)在UI线程中运行的过程。
对于线程的安全 Android也做出了限定 此外,Andoid UI工具包不是线程安全的。所以,你不能操作UI允许从一个工人操作的用户界面UI线程。因此,仅仅有两个规则,Android的单线程模型:
不阻塞UI线程
不从外部访问Android UI toolkit UI线程
使用工作线程来不阻塞UI线程
                                                             public void onClick(View v) {                                              new Thread(new Runnable() {                                              public void run() {                                              Bitmap b = loadImageFromNetwork("http://example.com/image.png");                                              mImageView.setImageBitmap(b);                                              }                                              }).start();                                             }                                
起初,这似乎工作好,因为它创建了一个新线程来处理网络操作。 然而,它违反了单线程模型的第二条规则:不要从外部访问Android UI toolkit UI片样品修改的ImageView工作线程而不是UI线程。 这可能导致未定义的和意想不到的行为,可以是困难和耗时的追踪。
要修复这个问题,Android提供了几种方法来从其他线程访问UI线程。这里是一个列表的方法,可以帮助:
  • Activity.runOnUiThread(Runnable)参数中的run方法是执行在UI线程中的
  • View.post(Runnable)
  • View.postDelayed(Runnable, long)延迟在run方法中执行
Handler 然而,随着操作的复杂性,这种代码也会变得很复杂,很难维护。 工作线程来处理更复杂的交互,您可能会考虑使用一个Handler在你的工作线程,从UI线程处理消息交付。 也许最好的解决方案,是扩展AsyncTask类,这简化了工作线程的执行任务,需要与用户界面交互。
AsyncTask AsyncTask允许您执行异步工作在你的用户界面。它执行阻塞操作在一个工作线程,然后在UI线程上公布结果,不需要你自己处理线程和/或处理程序。
使用它,您必须子类AsyncTask,实现doInBackground()回调方法,运行在一个后台线程池。更新UI,你应该实现onPostExecute(),交付结果从doInBackground()和在UI线程中运行,所以您可以安全地更新UI。然后您可以运行任务从UI线程通过调用execute()。
                                                             public void onClick(View v) {                                              new DownloadImageTask().execute("http://example.com/image.png");                                             }                                                                                          private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {                                              /** The system calls this to perform work in a worker thread and                                              * delivers it the parameters given to AsyncTask.execute() */                                              protected Bitmap doInBackground(String... urls) {                                              return loadImageFromNetwork(urls[0]);                                              }                                                                                           /** The system calls this to perform work in the UI thread and delivers                                              * the result from doInBackground() */                                              protected void onPostExecute(Bitmap result) {                                              mImageView.setImageBitmap(result);                                              }                                             }                                                                              

更多相关文章

  1. Android之 UI主线程ZT
  2. Android中的多线程
  3. 五.在Android中实现线程的方法
  4. android 多线程
  5. Android 应用程序进程启动过程源码分析
  6. 【android】欢迎来到Android多进程时代
  7. Android 多线程----AsyncTask异步任务详解

随机推荐

  1. Android中画面的布局工具
  2. Android(安卓)将Activity转化为DialogAct
  3. android:inputType常用取值
  4. UI_Layout 屬性”@+id”、”@id”和”@an
  5. Android(安卓)selector(背景选择器) , sh
  6. 【Android】Android(安卓)开机广播的使用
  7. Android(安卓)5.1 open data flow 数据开
  8. Android里merge和include标签的使用
  9. Android修改自己程序字体
  10. Android(安卓)Studio下添加assets目录