背景
本笔记源自平时课程《Introduction to Android development Openium / ISIMA》,描述android的一些基本理论知识。
Introduction
- Operating system based of Linux kernel
- Developt by the Open Handset Alliance (Google, Samsung, HTC, Intel, Motorola, Qualcomm. Google is the main contributor)
- Free software - Apache licence
Architecture
Android Architecture
Software components
- Activity
- Service
- Content Provider
- Broadcast Receiver
Activity
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)…
Activity = screen
An Activity can be in 4 different states:
- Running : displayed on screen and in foreground
- Paused : still displayed but not in foreground (dialog in front of it for example)
- Stopped : not visible anymore. State is saved. Can be killed by the system to save memory if needed
- Destroyed : deleted from memory. State can be saved
Service
A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
Service 和 Activity的关系图
4.3 Content Provider
Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.
When you want to access data in a content provider, you use the ContentResolver object in your applications Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.
- Structured interface for data access
- Easy loose coupling of code and data
- Most often used to access to a database
- Allow data sharing between applications
Broadcast Receiver
Component allowing to received all the broadcast messages on the device
Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.
There are following two important steps to make BroadcastReceiver works for the systen broadcasted intents:
- Creating the Broadcast Receiver.
- Registering Broadcast Receiver.
Examples:
- System start
- User position
- Battery level
- Connectivity change (Wi-Fi, 3G, no network)
Intents
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Intents data
An intent can contains a set of data
- Action (ACTION_VIEW, ACTION_EDIT, ACTION_MAIN)
- Data (Uri)
- Category (CATEGORY_LAUNCHER, CATEGORY_ALTERNATIVE)
- Type (picture, contact, text…)
- Extras (Bundle key/value)
- Component
Intents 类型
- Explicit Intent
Intent sent to a specific component and not an other - Implicit Intent
Generic message sent to the system and multiple application can answer
Intent example
explicit intent:1
2Intent i = new Intent ( mContext , MyActivity . class );
startActivity ( i );
implicit intent:1
2
3Intent i = new Intent ( Intent . ACTION_VIEW ,
Uri.parse ( " http :// www . google . fr " );
startActivity ( i );
data add:1
2
3
4Intent i = new Intent ( mContext , MyActivity . class )
i.putExtra ( " id_result " , 1234);
i.putExtra ( " data " , anObjet );
startActivity ( i );