Basics
- Android系统是’multi-user’的Linux系统,每个App是不同的’user’。
- 每个进程都有自己的虚拟机(Virtual Machine),每个App独立于其他App运行。
- 系统会设立一些需要权限(permission)的资源,拥有对应权限的App才能正常访问这些资源。
- 当内存不够时,Linux系统会自动销毁不必要的进程。
App Components
Four Components
Android四大组件包括:Activity, Service, Content provider, Broadcast receiver。
组件(component)不仅可以被自身App调用,也可以由其他App的组件调用。App components are the essential building blocks of an Android app.
Each component is a different point through which the system can enter your app.Activities
Activity是与用户进行交互的前台。An activity represents a single screen with a user interface.
Services
Service是后台运行的任务,不提供UI界面。A service is a component that runs in the background to perform long-running operations or to perform work for remote processes.
Content providers
A content provider manages a shared set of app data.
Broadcast receivers
A broadcast receiver is a component that responds to system-wide broadcast announcements.
Activating Components
Activiy, Service, Broadcast receiver均可以由Intent进行调用。- Activity: passing an Intent to
startActivity()
orstartActivityForResult()
. - Service: passing an Intent to
startService()
orbindService()
. - Broadcast receiver: passing an Intent to
sendBroadcast()
,sendOrderedBroadcast()
, orsendStickyBroadcast()
. - Content provider: calling
query()
on a ContentResolver.
- Activity: passing an Intent to
The Manifest File
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.
- Declare the app’s components.
- Identify any user permissions the app requires.
- Declare the minimum API Level
- Declare hardware and software features, such as a camera or bluetooth.
- API libraries the app needs to be linked against, such as the Google Maps library.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.train.eureka.intenttest">
<!-- Declare permissions -->
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<!-- Declare app requirements -->
<uses-feature android:name="android.hardware.camera.any"
android:required="true" />
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- Declare components -->
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<!-- Declare components capabilities -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
App Resource
在res目录下,包含着App需要的各种资源文件,包括drawable, layout, menu, mipmap, values。
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.