The Android system has four famous components: Activity, Service, Broadcastreceiver, ContentProvider. A commercial Android application, typically composed of several basic components. These four components are required to be registered in the manifest file Androidmanifest.xml when they are used, otherwise they will not be used. This section provides a brief introduction to these components, giving readers a general idea of what Android app development is all about.
Activities (activity)
Activity is the most direct-to-user component of an Android app that loads the view component, presents it to the user, and keeps the user engaged. All activity components need to inherit the activity class, which is an indirect subclass of content that wraps up some of the basic properties of the activity.
The view component is the base class for all UI components, container components, that is, it can be a layout container, or it can be a basic UI component within a layout container. The view component is typically defined by an XML layout resource file, and the Android system provides the corresponding implementation class for these view components. If you need to display the specified view component through an activity, call the activity's Setcontentview () method, which has multiple overloaded methods that can pass an XML resource ID or view object.
For example:
LinearLayout layout=new LinearLayout (this);
Setcontentview (layout);
Or:
Activity provides a user interface for Android apps that, when an activity is turned on, has its own life cycle. Activity classes also provide a corresponding approach to these lifecycles, which can be overridden if you need to respond to different life cycles of the activity. For most commercial applications, the entire system consists of multiple activity, which, after a gradual navigation of the activity in the app, creates a fallback stack for the activity, and the activity currently showing and gaining focus is at the top of the stack of the fallback stack.
Services (Service)
Service is mainly used in the background to complete some of the functionality without showing the user interface. Typically run in the background of the system, it generally does not need to interact with the user, so the service component does not have a user interface to present to the user. Service is primarily used to perform functions similar to downloading files, playing music, and so on without the user interface to interact with the user.
Similar to the activity component needs to inherit the activity class, the service component also inherits the service class, and the service class is also the indirect subclass of the context, which wraps some of the service's proprietary features. Once a service is run, it will have its own life cycle, and the service class provides a corresponding approach to its various lifecycles, and developers can override these life cycle methods in service classes To respond to the functional implementation of the service lifecycle.
Broadcast Receivers (Broadcastreceiver)
Broadcastreceiver is also an important component of the Android system, and Broadcastreceiver represents a broadcast receiver for receiving broadcasts sent by other components of the system, responding to them, or intercepting the continued propagation of the broadcast.
Broadcast is a system-level message, when the system environment changes will send some broadcasts for the corresponding program to receive responses, such as: Receive a text message, boot, shutdown, plug in the charger, plug in headphones, charging complete, etc., will send a broadcast for the need to listen to the application of such broadcasts to respond. In addition to the broadcast of some system events, developers can customize the broadcast content. In most cases, however, the application is developed primarily to accept and respond to system broadcasts, and it is seldom necessary to send custom broadcasts.
Using the Broadcastreceiver component to receive broadcasts is very simple, just implement your own broadcastreceiver subclass and rewrite the OnReceive () method to complete the broadcastreceiver, And for this broadcastreceiver is interested in what broadcast, it needs to be configured separately.
Content provider (ContentProvider)
The Android system, as an intelligent operating system, requires that the applications running in the system must be independent of each other and run in their own Dalvik VM instances. Under normal circumstances, there is no real-time data exchange between Android applications, and given that some applications need to be shared externally, the Android system provides a standard data interface ContentProvider, which is provided by the application ContentProvider , the exposed data of this application can be increased and censored in other applications.
Exposing the data interface to the application is very simple, just inherit the ContentProvider class, and implement the Insert (), delete (), update (), query () methods, so that the external application can be used for the application of data to be added and censored.
Intentions (Intent)
Although intent is not a component of Android apps, it does not need to be configured specifically in the manifest file, but it is very useful for Android apps. In addition to ContentProvider, the startup of other components needs to be specified by intent. Intent can not only explicitly specify an Android component to boot, but also provide a standard behavior, which is then selected by the Android system with the intent filter to launch the specified component to complete the task. and intent in the process of opening the component, the transfer of data between the various components.
Summary
This chapter briefly introduces the history and status of Android, introduces the architecture of Android and Dalvik VM VMs, and finally introduces four components in Android development. By reading this chapter, we have a general understanding of the history and status of Android, System architecture, and basic components, which is very helpful for the rest of the book.
01_android Application Development Environment _02_android basic components for Applications