Android Component Design

Source: Internet
Author: User

The Android component design model is the basis for Android program development and understanding of the Android security model.
Android applications, defined in official documents, refer to the Code logic contained in an APK file with a suffix. An Android application consists of several components. The four components of an Android application are Activity, Services, Content providers, and Broadcast receivers. One problem is, what is the difference between a component and a common Android JAVA object? First, the components must be defined in the configuration file of the Android application. They are scheduled by the system and have a unique lifecycle. Second, the components are the minimum granularity units of the Android application security management.

Before you understand the technical details of a component, you must first understand the running environment of the component.

Process running environment of Android Components

(1) Android is a multi-user Linux operating system. Each application runs with a different user ID. Android provides Linux operation-level permission protection for data files of applications, and grants only the Application User ID with access permissions, programs Running with other user IDs cannot access the data protected by programs beyond the permitted permissions.

(2) Each process has an independent JAVA virtual machine, and different applications are isolated in an independent JAVA virtual machine.

(3) by default, each application runs in an independent process. When any component needs to run, Android starts the application process. When all application components in the process stop running, Android is responsible for stopping the application process and releasing resources.

(4) If two applications need closely coupled data of the shared application, they need to be scheduled to run in the same application process. At this time, the two applications must have the same signature.

Four main components of Android

In the famous "Inside the Android Application Framework" material, the Android program composition is explained as follows:

 

 

Activity is a basic Android program component that contains display windows and a basic unit of human-computer interaction. Every Activity is a subclass of android. app. Activity. The Activity has a runtime context and a unique life cycle. The system schedules the Activity as needed. The Activity can be started by clicking the system desktop icon or by other applications.

The Service component does not contain a human-computer interaction interface. It is a component that executes a long-term task in the background. Service components can be started by other components in the application. Another usage of the Service component is to bind an IPC (inter-process communication) interface and support remote calls. The Service component supports any type of background tasks, including executing network transactions, playing music, and downloading background files.

The Service component is very special in that it runs in the background but shares the application's process space and occupies the Main Thread ). In the official development documentation, this point is highlighted. If you need to execute long-term or high-intensity tasks in the Service, we recommend that you create another working thread to complete it. Otherwise, it will slow down or block the response efficiency of other Activity components in the application process, and bring a poor user experience.

The Content Provider component is used to manage data that can be shared in an application, such as files and databases. If the data is only used in this application, you can directly use SQLiteDatabase without the Content Provider. One feature of Content Provider is that you do not need to write access clients. During access, the client application obtains the ContentResolver object in the context. This object is responsible for communication and interaction with the Content Provider corresponding to a specific Content URI. Android uses the Content Provider mechanism to save audio, video, images, and personal contact information.

The Broadcast receivers component is used to receive event declarations within the system range. For example, the screen is turned off and the battery is in a low-power state. Applications can also initiate event broadcasts for some occasions. For example, a data has been downloaded and related applications are notified to read the data. Although Broadcast receivers does not contain human-computer interaction windows, it can notify users of Broadcast events in the status bar of Android. The most common use of Broadcast receivers is that after receiving Broadcast events, Broadcast receivers passes control of execution to other components, for example, starting a Service to execute a task for Broadcast events.

 

Key features of Android Component Design

The key feature of Android component design is that any program can start components of another program. For example, if a team develops a program that includes opening a camera and capturing a photo, the team does not need to re-develop the function if a series of components of another application are fully implemented, you only need to directly use the application components. The so-called "direct use" does not even need to link existing application components into new programs. How can this be done? You can get the answer from the communication mechanism of Android components.

It can be summarized from the materials that Android has several component communication mechanisms: Intent mechanism, IPC Binding mechanism, and ContentResolver. The Intent mechanism can evoke three Android components: Activity, Service, and Boardcast Receiver. It is the main communication method between Android components and is an indirect communication method. The IPC Binding mechanism uses remote function call methods to communicate with Service components. ContentResolver is used to communicate with the ContentProvider component.

Communication Mechanism of Android Components

Intent is an asynchronous event object between Android components, carrying the "Operation Intent" of the sending component ". Translate to natural language: Send the Intent component to expect a specific component (explicitly specified) or the system to help find the appropriate component (implicitly specified) to complete a specific task, after the system finds the target component, if it is not in the running state, it is responsible for creating the component and scheduling it to the running state to execute the specified task. The Intent mechanism ensures that Android can reuse mature components of the system or other installed applications to the maximum extent.

For the Activity component and Service component, the received Intent object contains an operation (for example, View and Send), The URI of the data object corresponding to the operation, and necessary parameters. For example, you can view an image and open a webpage. A common usage is to send an Intent object to an Activity to require it to complete a task and include the operation result data in a "Intent" object and return it to the sender. For example, many applications send Intent messages to select contacts from the phone book and return the selected results.

For Broadcast receivers, the Intent it receives contains Broadcast event declarations. For example, in a low-power state, its Intent only contains an operation string, indicating that the device is in a low-power state.

The Service component supports the remote function call interface, which is the Binding usage of IPC. This is a special communication mode for Service components to communicate with other components.

The Content Provider component cannot be awakened by Intent. ContentProvider can only communicate with ContentResolver and wake it up. ContentResolver processes the entire process of communication with ContentProvider. components that communicate with ContentProvider do not need to add additional logic and only call ContentResolver.

 

 

Android component composition example

The following is an example of a program component. The basic functions of the program are as follows: view the geographical location of a friend, display it as a map, and prompt the user when a friend nearby passes. The entire human-computer interaction process involves three applications. The first application is FriendTracker, which tracks the location of the user and friends, and the second application is FriendViewer, which allows you to view the location of the friend, the third application is the system Contact application, which is used to view contacts. In the component structure analysis, the light-red ry represents an Android component, the blue ry represents a common JAVA object, and the line of the blue arrow represents the communication mode between components.

 

The entire function is completed by three applications. The first application, the FriendTracker APP, is responsible for tracking the geographical location of myself and my friends, and obtaining my longitude and latitude information from the local LocationManager, the longitude and latitude of a friend are obtained from the Network Center Server.

The FriendTracker APP consists of a FrendTracker Activity component, a FriendTracker Service component, and a Friend Provider component. The FrendTracker Activity component provides a window for One-user interaction, allowing users to start or stop the "friend tracking" Service. At this time, the FrendTracker Activity sends Intent to start the FriendTracker Service component through the startService function.

After the FriendTracker Service is started, it starts the LocationManager, starts the timer, starts the working thread to obtain the geographical location information of friends from the network, writes the information to the database through the FriendProvider, and then runs the system scheduling, which may enter sleep. When the timer expires, an Intent will be sent to the system. This Intent will wake up the FriendTracker Service and then start the working thread again to obtain the geographical location information of friends. When the user's geographic location information changes, the FriendTracker Service calculates the location with each friend. If a friend is found within 500, a broadcast Intent is sent. The Intent calls the FriendViewer App. The APP contains a FriendViewer Activity, which reads and displays the location information of each friend from the FriendProvider. When selecting a friend from the FriendViewer Activity window list, send Intent to trigger the FriendMap Activity to wake up and display the location where the friend is located. Of course, you can also select a friend from the FriendViewer Activity window list to view the friend's detailed information. At this time, sending Intent triggers the system APP. Www.2cto.com

Here, we can see that the FriendTracker Service starts other threads to complete their tasks. The reason is shown in the preceding description of the Service.

However, the functions of an entire application are scattered among the three sub-applications to better share and reuse and upgrade management.

The complexity is that most of the communication relationships between each component mean permission management, especially cross-application component communication. Permission management of Android components is complicated. This issue is reserved for the next study.
Author: shallon_luo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.