Home > Common Problem > What are the four major components of android

What are the four major components of android

青灯夜游
Release: 2022-11-24 19:14:44
Original
45960 people have browsed it

Four major components: 1. Activity component, which is a separate window and the program flow must be run in [Activity], so it is the most basic module. 2. The service component is used to complete user-specified operations in the background. 3. The content provider component will prepare a content window for all applications and retain databases and files. 4. The broadcast receiver component is a mechanism for transmitting information between programs. Its function is to receive or send notifications.

What are the four major components of android

#The operating environment of this tutorial: Android 13 system, Xiaomi 12 mobile phone.

The four major components of Android are activity, service, content provider, and broadcast receiver.

1. Detailed explanation of the four major components of android

1. Activity

Activity It can be regarded as the foundation of the Android system. Only on this foundation can other work be carried out, because all programs running in the Android system must run in [Activity], so it is the most basic module. . It functions as a frame or page, and each program will be composed of multiple [Activities].

(1) An Activity is usually a separate screen (window).

(2) Activities communicate through Intent.

(3) Every Activity in the android application must be declared in the AndroidManifest.xml configuration file, otherwise the system will not recognize or execute the Activity.

2. service

Service is a very important component in Android. Its status and priority are similar to activities. However, Service cannot run by itself. It only runs in the background on Android. Its function is to interact with other components of Android. For example, when we open the music player on the phone and put it in the background, the music played at this time is controlled by the Service.

1) Service is used to complete user-specified operations in the background. Service is divided into two types:

  • started (started): When an application component (such as activity) calls the startService() method to start the service, the service is in the started state.

  • bound (binding): When the application component calls the bindService() method to bind to the service, the service is in the bound state.

2) The difference between startService() and bindService():

  • started service (start service) is the startService() method called by other components Started, which causes the service's onStartCommand() method to be called. When a service is in the started state, its life cycle is independent of the component that started it and can run in the background indefinitely, even if the component that started the service has been destroyed. Therefore, the service needs to be stopped by calling the stopSelf() method after completing the task, or by other components calling the stopService() method.

  • Use the bindService() method to enable the service. The caller and the service are bound together. Once the caller exits, the service will terminate. "death" characteristics.

3) Developers need to declare all services in the application configuration file, using the tag.

4) Service usually runs in the background. It generally does not need to interact with users, so the Service component does not have a graphical user interface. Service components need to inherit the Service base class. Service components are usually used to provide background services for other components or monitor the running status of other components.

3. Content provider (content provider)

The content provider component is specially designed for third-party applications. It is very flexible and very important. , it will prepare a content window for all applications, and retain databases and files. Its function is that when we use these third-party software, we can effectively access and protect the data inside.

1) The android platform provides Content Provider to provide the specified data set of an application to other applications. Other applications can obtain or store data from this content provider through the ContentResolver class.

2) Content providers are only needed if data needs to be shared between multiple applications. For example, address book data is used by multiple applications and must be stored in a content provider. Its benefit is to unify the way data is accessed.

3) ContentProvider implements data sharing. ContentProvider is used to save and obtain data and make it visible to all applications. This is the only way to share data between different applications because Android does not provide a common storage area that all applications can access.

4) Developers will not directly use objects of the ContentProvider class. Most of them implement operations on ContentProvider through ContentResolver objects.

5) ContentProvider uses URI to uniquely identify its data set. The URI here is prefixed with content://, indicating that the data is managed by ContentProvider.

4. broadcast receiver

In the Android system, the broadcast receiver is not directly visible. It is a mechanism for transmitting information between programs. Its function is to receive or send notifications. In layman's terms, a broadcast receiver is more like a delivery component. It can receive information and even filter it and then respond.

1) Your application can use it to filter external events and only receive and respond to external events of interest (such as when a phone call comes in, or when the data network is available). Broadcast receivers have no user interface. However, they can start an activity or service in response to the information they receive, or use a NotificationManager to notify the user. Notifications can be used in many ways to attract the user's attention, such as flashing the backlight, vibrating, playing sounds, etc. Generally speaking, you put a persistent icon on the status bar that the user can open and get the message.

2) There are two methods for registering broadcast receivers, namely dynamic registration in the program and static registration in the AndroidManifest file.

3) The characteristic of dynamically registered broadcast receivers is that when the Activity used to register is turned off, the broadcast will become invalid. Static registration does not need to worry about whether the broadcast receiver is turned off. As long as the device is turned on, the broadcast receiver is also turned on. That is to say, even if the app itself is not started, the broadcast subscribed by the app will affect it when triggered.

2. Summary of the four major components of android:

1. Registration of the four major components

4 The major basic components need to be registered before they can be used. Each Activity, service, and Content Provider need to be configured in the AndroidManifest file. Activities, services, and content providers that are not declared in the AndroidManifest file will not be visible to the system and therefore will not be available. The registration of broadcast receivers is divided into static registration (configured in the AndroidManifest file) and dynamic creation through code and registration to the system by calling Context.registerReceiver(). It should be noted that the broadcast receiver configured in the AndroidManifest file will remain active as the system starts, and will be triggered as long as an interested broadcast is received (even if the program is not running).

2. Activation of the 4 major components

Activation of content provider: After receiving a request from ContentResolver, the content provider is activated. The other three components activities, services and broadcast receivers are activated by an asynchronous message called an intent.

3. Closing of the 4 major components

The content provider is only activated in response to a request from ContentResolver. A broadcast receiver is only activated in response to broadcast messages. Therefore, there is no need to explicitly close these components. Activity closure: You can close an activity by calling its finish() method. Service shutdown: For services started through the startService() method, the Context.stopService() method must be called to shut down the service. For services started using the bindService() method, the Contex.unbindService() method must be called to shut down the service.

4. Tasks (activity stack) in android

a) A task is actually a stack of activities. It consists of one or more Activities, which together complete a complete user experience. The bottom of the stack is the Activity that starts the entire task, and the top of the stack is the currently running Activity that the user can interact with. When one activity starts another, the new activity is pushed onto the stack and becomes the currently running activity. The previous activity remains on the stack. When the user presses the BACK key, the current activity pops off the stack and the previous one returns to the currently running activity. What is saved in the stack is actually an object. The activities in the stack will never be rearranged, but will only be pushed or popped.

b) All activities in the task move as a whole. The entire task (i.e. activity stack) can be moved to the foreground or retreated to the background.

c) The Android system is a multi-task (Multi-Task) operating system, which can execute multiple other programs while listening to music on your mobile phone. Every time an additional application is executed, more system memory will be consumed. When too many programs are executed at the same time, or the closed program does not release memory correctly, the system will feel slower and slower, or even unstable. In order to solve this problem, Android introduced a new mechanism, namely Life Cycle.

For more related knowledge, please visit the FAQ column!

The above is the detailed content of What are the four major components of android. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template