Android之旅[2] -Application Framework

本文主要是Inside the Android Application Framework的随笔记录。 

Managed Component Lifecycle

  • An Android APK is a collection of components

  • Components share a set of resources
    • Databases, preferences, file space, etc.

    • Also: a Linux process.      (一个App占用一个进程空间)

  • Every Android component has a managed lifecycle

今天听别人说iOS只有一个线程是Active的,当用户切换到另外一个应用后,原来的应用停止了,不像Android会继续在后台运行,求证!

Activities, Tasks, Processes

  • An Activity is a “molecule”: a discrete chunk of functionality
  • A task is a collection of Activities
  • A “process” is a standard Linux process

clipboard

Task是一个跨进程的概念,可以将它理解为实现特定应用的全部组件集合。一个apk可能占用多个进程,可以在AndroidManifest中指定。进程的概念是操作系统级别的,做Android应用的视角主要集中在四个基本组件上:Activity、Service、Broadcast Receiver和Content Provider。

Activities:

  • ...a concrete class in the API
  • ...an encapsulation of a particular operation
  • ...run in the process of the .APK which installed them
  • ...optionally associated with a window (UI)
  • ...an execution Context

Task:

  • ...more of a notion than a concrete API entity  概念
  • ...a collection of related Activities  一些Activities的集合,上图也反映出来了
  • ...capable of spanning multiple processes  能够跨进程
  • ...associated with their own UI history stack  有一个UI栈
  • ...what users on other platforms know as “applications” 就是经常说的App应用

Process:

  • Android process == Linux process
  • By default, 1 process per APK
  • By default, 1 thread per process
  • All* components interleave events into the main thread  主线程即UI线程有一个消息队列,与此对应的还有一个Looper来不停地从MessageQueue中取出消息,然后交由不同的Hander来处理消息。Hander同时也用来发送消息。

Process Lifecycle

  • A process is started for a given user ID when needed
    • Binding to a Service
    • Binding to a ContentProvider
    • Starting an Activity
    • Firing an IntentReceiver
  • Remains running until killed by the system

Activity Lifecycle

activity_lifecycle

  • Starting up
    • onCreate(): first method called during lifetime, with prior state
    • onStart()/onRestart(): signal that execution is beginning
    • onResume(): signals that a previous pause is being undone
  • Normal execution
    • onFreeze(): save UI state (NOT intended to save persistent data)
    • onPause: signals loss of focus and possible impending shutdown
  • Shutting down
    • onStop()/onDestroy(): final shutdown and process termination
    • Not guaranteed to be called (and usually not, except on finish()...)

Activity的生命周期很低地方都能找到。

Threading

  • Each process has one thread (by default)
  • Most components share the single thread
  • Services and ContentProviders sometimes do not

线程主要包含三种:一是主线程(UI线程),二是界面无关的子线程,三是界面相关的子线程。三种线程分别对应着ActivityThread、HanderThread和AsyncTask。

Looper

  • Each thread has a Looper to handle a message queue
  • Events from all components are interleaved into Looper
    • e.g. View UI events, IntentReceivers firing, etc.
  • Loopers cannot accommodate multi-threaded access
    • They are designed to play nicely with MessageHandlers

clipboard[1]

Views:

  • Views use Looper messages to fire events
  • Since Loopers are 1:1 with threads, the View tree is too
  • Threads you create cannot directly touch a View
  • But, you can create a new Looper for your own thread

Service Lifecycle

  • Started by some other Component
    • Either explicitly, or implicitly by binding to it
  • Explicitly-started Services run until explicitly shut down
    • (or killed by the system during a memory crunch)
  • Implicitly-started Services run til the last client unbinds

Process Resource Management

  • Spawned by the special “Zygote” process

    • Process + pre-warmed Dalvik VM == responsiveness    Zyote进程空间中包含Dalvik VM的代码,fork新进程时可以共享。

  • Process runs under user ID unique to system

    • Process + User ID == security   每个进程在应用安装时都会分配一个Linux用户ID,标识应用程序身份

Process Secutiry

  • Each application is given a unique user ID

    • No exceptions!

    • ...except these: init, Zygote, and the main runtime

  • Each application has direct access only to its own data

  • Other apps’ resources are available only via defined, explicitly-exposed APIs

    • i.e. Issuing Intents, binding to Services or ContentProviders

IPC

clipboard[2]

Binder

  • All IPC goes through “The Binder”
    • Binder is implemented as a kernel module + system lib
    • Supports sophisticated cross-process data transport
  • The framework APIs “know” how to use Binder
  • Generally two entry points: Bundles & Parcelables

Parcelable

  • A Parcelable is a class which can marshal its state to something Binder can handle -- namely, a “Parcel”
  • Standard Java serialization has semantics Parcelables don’t need
    • Supporting full serialization would mean wasting CPU cycles

Bundles

  • Bundles are typesafe containers of primitives
    • That is, C-like primitives: ints, strings, etc.
  • Simple data-passing APIs use Bundles
    • Think of onFreeze() as passing data to your future self
  • Flat structure permits optimizations like memory-mapping

Parcel意思是打包,相当于Java的序列化,它是通过Binder进行RPC必要的数据传输手段。Parcel和Bundle不能不好区分,可以用Android代码的注释来解释下它们的区别:

Bundle:A mapping from String values to various Parcelable types.

Parcel:Container for a message (data and object references) that can be sent through an IBinder.  A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the general interface), and references to live objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.

AIDL“Android Interface Definition Language”

  • Used to build developer-friendly APIs using Parcelables
  • Preferred way to expose structured, complex-typed APIs
  • Compromise between efficiency and Java usability

The Activity Manager manages the lifecycle of the applications and provides a common navigation backstack of applications that are running in dierent processes.

The Package Manager maintain track of all applications that are installed in the device.

The Windows Manager manages screen of mobile device and creates surfaces (an allocated memory block) for all running applications.

The Telephony Manager support applications to access the information regarding telephony services. Access to some telephony

information is protected and some applications may not have permission to access such protected information (Constrain to permissions declared in its manifest le).

Content Providers supports the sharing and accessing of data among applications; suppose the messaging service is an application that can access the data of other application contacts. In Android the term resources refers to the non code external assets.

The Resource Manager provides access to such external resources to native Android application at built time. Android supports a number of different kinds of resource files, including XML (use to store anything, other than bitmaps and Raw), Bitmap (Use to store images), and Raw files (other resources such as sound, string, etc).

The development of new Android applications requires access to views of previously built application. The View System provides access to some extensible set of views, such as lists, text boxes, grids, embedded browser, and button.

Notication Manager allows application to notify users about events that take place. The notication can be inform of vibration, flashing LEDs of mobile device, playing specific sound against specific event, or could be an icon (persistently displayed into the status bar).

你可能感兴趣的:(Android之旅[2] -Application Framework)