Four major components in Android

Four major components of Android:

1. Classification: Activity, Service, Broadcast Receiver, Content Provider.

Insert image description here

1、Activity

(1) An Activity is usually a separate screen (window).
(2) Activities communicate through Intent.
(3) Every Activity in an Android application must be declared in the AndroidManifest.xml configuration file, otherwise the system will not recognize or execute the Activity.

2、Service

(1) Service is used to complete user-specified operations in the background. There are two types of services:

(a) started: When an application component (such as activity) calls the startService() method to start the service, the service is in the started state.
(b) bound: 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():

(a) The started service (started service) is started by other components calling the startService() method, 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.
(b) Use the bindService() method to enable the service. The caller and the service are bound together. Once the caller exits, the service will be terminated. This has the characteristic of "if you don't want to live at the same time, you must die at the same time".

(3) Developers need to declare all services in the application configuration file, using tags.
(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

(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 realizes 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

(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 4 major components

The four 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 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 4 major components

Content providers are only activated in response to requests 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 in android (activity stack)

(a) A task is actually a stack of activities, which consists of one or more activities that 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 operating system that 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.

3. Two ways to start Android services

Foreword: Service is one of the four major components of Android and is also an executable program. Services are a solution for running programs in the background in Android. They are very suitable for performing tasks that do not require interaction with users and require long-term running. There are two ways to start Service. Let’s summarize the differences between the two ways.

1. The first way to start Service

Use the start method to start the service

Basic usage of services: 1. Create a class that inherits Service, indicating that this is a service.

2. Register in AndriodMainfest.xml. Each service needs to be registered to take effect. It will be registered by default in Android Studio.

3. Use the startService(Intent) method of Context to start the Service.

4. Use stopService(Intent) to stop the service.

Note: The life cycle of the Service started using this start method is as follows: onCreate()—>onStartCommand()—>onDestory(). onCreate() will be executed once when the service is created, and onStartCommand() will be executed every time the service is started. When the service stops, onDestroy() will be called.

Features: Although the service is started in the activity, after the service is started, there is basically no relationship between the activity and the service. After the service executes onStartCommand() after it is started, it will always be running and the activity cannot be controlled. The onBind() method can make the relationship between activities and services closer.

2. The second way to create Service

Use bind to start the service

Steps to use Service: 1. Create a class that inherits Service, indicating that this is a service.

2. Register in AndriodMainfest.xml. Each service needs to be registered to take effect. It will be registered by default in Android Studio.

3. Use the bindService(Intent, ServiceConnection, int) method of Context to start the Service

4. When no longer in use, call the unbindService(ServiceConnection) method to stop the service.

The life cycle of a Service started using this start method is as follows:

onCreate()—>onBind()—>onunbind()—>onDestory()

Note: The bound service will not call the onstart() or onstartcommand() method

Features: Use the bind method to start the service and bind the service. If the caller hangs up, the service will also hang up.

The binder can call methods in the service.

4. Application life cycle

Recently, I have been studying Replugin plug-in development and found that initialization is called in Application, so I want to learn some knowledge about Application.

1. Related methods

onCreate(): called when creating

attachBaseContext(Context base): Inherited from ContextWrapper, sets the most basic context context

onTerminate(): will be called when the application is completely closed. When tested with the simulator, no callback was found.


onConfigurationChanged(@NonNull Configuration newConfig): Call onLowMemory() when the configuration changes : When the system memory is low, it will notify you to release resources.

onTrimMemory(int level): Notifies to release memory. During actual testing, this method will be called when the application is closed, so resources can be released here.

2. Life cycle

When the application is opened for the first time:
Operation: Click to open the application, press the return key to exit the application

Insert image description here

Open the app a second time (without closing it completely)

Insert image description here

The onTrimMemory() method will only be called when closing. The previous onCreate() and attachBaseContext() will not be called.

Open the app multiple times (without completely closing the app)

Insert image description here

All will only call the onTrimMemory() method

Close the app completely and reopen it

Insert image description here

It will have the same life cycle as when it was first opened.

Summary: Third-party libraries are basically initialized in the onCreate() method; if you want to release resources, it is best to do it in the onTrimMemory() method, because onTerminal() has not been called; but third-party libraries need to pay attention to releasing resources, because onCreate () The second opening will not continue execution. At this time, you need to check whether it has been initialized, otherwise an uninitialized crash will occur.

Guess you like

Origin blog.csdn.net/zxz_zxz_zxz/article/details/131046310