https://developer.android.com/guide/components/fundamentals.html
Android SDK tools 把你的代码、数据和资源文件编译成一个APK文件。这个APK文件包含了APP需要的所有文件,而且Android设备使用APK文件来安装APP。
一旦安装完成,每个Android APP都生存于自己的沙盒中:
- The Android operating system is a multi-user Linux system in which each app is a different user.
- By default, the system assigns each app a unique Linux user ID (the ID is used only by the system and is unknown to the app). The system sets permissions for all the files in an app so that only the user ID assigned to that app can access them.
- Each process has its own virtual machine (VM), so an app's code runs in isolation from other apps.
- By default, every app runs in its own Linux process. Android starts the process when any of the app's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other apps.
虽然每个app都有自己生存的沙盒,但是他们之间也可以交流或者获取系统服务:
- It's possible to arrange for two apps to share the same Linux user ID, in which case they are able to access each other's files. To conserve system resources, apps with the same user ID can also arrange to run in the same Linux process and share the same VM (the apps must also be signed with the same certificate).
- An app can request permission to access device data such as the user's contacts, SMS messages, the mountable storage (SD card), camera, Bluetooth, and more. The user has to explicitly grant these permissions. For more information, see Working with System Permissions.
APP组件
Android有4种组件,分别是Activities、Services、Content providers、Broadcast receivers。下面简要介绍一下这四种组件。
Activities
- An activity represents a single screen with a user interface.
Services
- A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface.
Content providers
- A content provider manages a shared set of app data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your app can access. Through the content provider, other apps can query or even modify the data (if the content provider allows it).
- Content providers are also useful for reading and writing data that is private to your app and not shared.
- A content provider must implement a standard set of APIs that enable other apps to perform transactions.
Broadcast receivers
- A broadcast receiver is a component that responds to system-wide broadcast announcements.
- Broadcasts originate from the system and apps.
- A broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work.
- Each broadcast is delivered as an Intent
object.
A unique aspect of the Android system design is that any app can start another app’s component.
When the system starts a component, it starts the process for that app (if it's not already running) and instantiates the classes needed for the component.
Because the system runs each app in a separate process with file permissions that restrict access to other apps, your app cannot directly activate a component from another app. The Android system, however, can. So, to activate a component in another app, you must deliver a message to the system that specifies your intent to start a particular component. The system then activates the component for you.
激活组件
Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called anintent. Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your app or another.
An intent is created with an Intent object, which defines a message to activate either a specific component or a specific type of component—an intent can be either explicit or implicit, respectively.
For activities and services, an intent defines the action to perform (for example, to "view" or "send" something) and may specify the URI of the data to act on (among other things that the component being started might need to know). In some cases, you can start an activity to receive a result, in which case, the activity also returns the result in an Intent.
For broadcast receivers, the intent simply defines the announcement being broadcast (for example, a broadcast to indicate the device battery is low includes only a known action string that indicates "battery is low").
The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver. The content resolver handles all direct transactions with the content provider so that the component that's performing transactions with the provider doesn't need to and instead calls methods on the ContentResolver object. This leaves a layer of abstraction between the content provider and the component requesting information (for security).
他们各自有几种方法来激活组件:
- You can start an activity (or give it something new to do) by passing an Intent to startActivity() or [startActivityForResult()](https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int))
(when you want the activity to return a result). - You can start a service (or give new instructions to an ongoing service) by passing an Intent to startService(). Or you can bind to the service by passing an Intent to [bindService()](https://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)).
- You can initiate a broadcast by passing an Intent to methods like [sendBroadcast()](https://developer.android.com/reference/android/content/Context.html#sendBroadcast(android.content.Intent), [sendOrderedBroadcast()](https://developer.android.com/reference/android/content/Context.html#sendOrderedBroadcast(android.content.Intent, java.lang.String)), or sendStickyBroadcast().
- You can perform a query to a content provider by calling [query()](https://developer.android.com/reference/android/content/ContentProvider.html#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)) on a ContentResolver.
AndroidManifest.xml文件
Before the Android system can start an app component, the system must know that the component exists by reading the app's AndroidManifest.xml file (the "manifest" file). Your app must declare all its components in this file, which must be at the root of the app project directory.
声明组件
The primary task of the manifest is to inform the system about the app's components. For example, a manifest file can declare an activity as follows:
...
You must declare all app components this way:
-
elements for activities -
elements for services -
elements for broadcast receivers -
elements for content providers
Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run. However, broadcast receivers can be either declared in the manifest or created dynamically in code (as BroadcastReceiver objects) and registered with the system by calling [registerReceiver()](https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)).
声明组件功能
An implicit intent simply describes the type of action to perform (and, optionally, the data upon which you’d like to perform the action) and allows the system to find a component on the device that can perform the action and start it. If there are multiple components that can perform the action described by the intent, then the user selects which one to use.
The way the system identifies the components that can respond to an intent is by comparing the intent received to the intent filters provided in the manifest file of other apps on the device.
For example, if you've built an email app with an activity for composing a new email, you can declare an intent filter to respond to "send" intents (in order to send a new email) like this:
...
Then, if another app creates an intent with the ACTION_SEND action and passes it to startActivity(), the system may start your activity so the user can draft and send an email.
APP资源文件
An Android app is composed of more than just code—it requires resources that are separate from the source code, such as images, audio files, and anything relating to the visual presentation of the app. Using app resources makes it easy to update various characteristics of your app without modifying code and—by providing sets of alternative resources—enables you to optimize your app for a variety of device configurations (such as different languages and screen sizes).
For every resource that you include in your Android project, the SDK build tools define a unique integer ID, which you can use to reference the resource from your app code or from other resources defined in XML.
One of the most important aspects of providing resources separate from your source code is the ability for you to provide alternative resources for different device configurations.
Android supports many different qualifiers for your alternative resources. The qualifier is a short string that you include in the name of your resource directories in order to define the device configuration for which those resources should be used. 例如:
To change the layout depending on the orientation, you can define two different layouts and apply the appropriate qualifier to each layout's directory name. Then, the system automatically applies the appropriate layout depending on the current device orientation.