SoFunction
Updated on 2025-03-02

Detailed explanation of the four major application components of Android

A core feature of Android is that an application can serve as an element in other applications and can provide data to other applications. For example, if a program needs to use certain controls to load some pictures, and another program has developed this function and can be used by other programs, you can directly call the function of that program using cross-process communication method instead of developing another one by yourself. To achieve such functionality, the Android system must be able to start its process when any part of the application is needed and instantiate that part of the Java object. Therefore, unlike most programs in other systems, Android programs do not have only a single entry point, but they have components that are necessary for system instantiation and operation. Android provides 4 major components; in addition to BroadcastReceiver, the four major components in Android must be registered in the Activity, Service, and ContentProvider, and BroadcastReceiver can be registered in the file, or in Java code or kotlin code; after Android 8.0, the static registration broadcast reception in the file failed because the official optimization of power consumption and avoids APP abuse of broadcasting.

1、Activity

Activty is a display component, and Activity provides users with a visual user interface. For example, a dialing program may have an Activity to display contacts that can make calls, the second Activity is used to create a new contact and write information, and other Activity is used to view specific contacts or change contact information. Although the user interface provided by each Activity in the application is very aggregation, each Activity is independent of other Activity. Each instantiated Activity is a subclass of Activity. Intent can trigger the startup of the Activity. Intent can be divided into explicit Intent trigger and implicit Intent trigger; explicit Intent trigger can explicitly point to the Activity component, expressed in the following code:

 Intent in = new Intent(this,)
 (in)

An implicit Intent trigger is a target component that points to one or more activities. It can also have no target activity. Its implicit trigger is represented by the following code:

Intent intent = new Intent();
("");
("");
(".APP_MAPS");
(intent);

2、Service

Service is a background task-based component that runs in the background and is used to process a series of computing tasks or play background music when processing other things. Each service is extended from the Service class; the service component and the Activity component are opened differently. Activity has only one startup state, which is represented by the following code:

Intent in = new Intent(this,)
startActivity(in)

There are two types of service that are enabled. When in the startup state, it can do some background tasks without interacting with the user interface. Its life cycle is as long as the application. Playing music by a multimedia player is a very good example of applying service. A multimedia player program may contain one or more activities through which users select and play music. However, music playback does not require an activity to handle, because the user may want the music to continue playing, and will not stop even if he exits the player and executes other applications. To keep the music playing all the time, the multimedia player Activity may start a Service to play music in the background. The Android system will keep the music playback service running, even after the activity that starts this service exits. Its startup can be expressed as follows:

Intent in = new Intent(this,)
(in)

When it is in a bound state, it can do some background tasks or interact with the user interface. Its life cycle is as long as the user interface. Its binding can be expressed by the following code:

ServiceConnection mBinderPoolConnection = new ServiceConnection() {
  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {

  }
 };
 
Intent intent = new Intent(mContext, ); 
(intent,new ServiceConnection(),Context.BIND_AUTO_CREATE);

When enabled in the above 2, no matter which one is, you cannot directly perform time-consuming operations in the Service, because it runs in the main thread. If you have to perform time-consuming operations, you should open a worker thread for it to execute.

3、BroadcastReceiver

Generally, no tasks are performed, but only components such as receiving and corresponding broadcast notifications are received. Most broadcast notifications are generated by the system, such as changing the time zone, alarm reminder, user selecting an image, or user changing language preferences. Applications can also send broadcast notifications, such as notifying other applications that some data has been downloaded to the device and can be used; an application's BroadcastReceiver responds to its notifications, and all BroadcastReceiver implementation classes are extended from the BroadcastReceiver class. BroadcastReceiver is suitable for communication between different components and different processes. It has no user interface because it works inside the system. The following are two registration methods. The first is static registration. It is completed in a file. It will be parsed by the application when installing the application. It can receive broadcasts without starting the application. It is represented by the following code to listen for changes in wifi status:

<receiver android:name=".">
 <intent-filter>
   <action android:name=".RSSI_CHANGED" />
   <action android:name=".STATE_CHANGE" />
   <action android:name=".WIFI_STATE_CHANGED" />
  </intent-filter>
</receiver>

From the above code, we can find that the matching of the reception process is described by <intent-filter>, and it can be concluded that broadcast is a low-coupled observer pattern.
Another way is to register dynamically. You need to start the application before you can receive the broadcast. Registration is completed in Java code, and its dynamic registration is represented by the following code:

public class MyBroadcastReceiver extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent){
   
  }
 }
 
 
 MyBroadcastReceiver receiver = new MyBroadcastReceiver();
 IntentFilter filter=new IntentFilter();
 (".ACTION_1");
 (".ACTION_2");
 (receiver,filter);

Sending broadcasts can be implemented as follows:

Intent intent = new Intent();
(".ACTION_2");
(intent);

In the above two broadcast registration methods, the reception of broadcasts cannot be performed in time-consuming operations, because the method of receiving broadcasts is called in the main thread.

4、ContentProvider

ContentProvider is a shared data component. Applications can access data from other applications, including private data from other applications. Like Service, it has no user interface. It needs to implement insert, update, delete and query methods internally. It uses a data set internally and has no requirements for data sets. ContentProvider communicates across processes. When the Android system receives a request that requires a component to process, Android will ensure that the host process of the component that handles the request is already running. If not, the process will be started immediately. ContentProvider provides an external interface ContentResolver to access data for other processes. The following part of the code simply represents the use of the query method:

Uri bookUri = ("content:///data");
ContentResolver cr = ();
Cursor bookCursor = (bookUri,new String[]{"_id","name"},null,null,null);
while (()) {
 int id = (0);
 String name = (1);
}

In the above code, first, you need to create the Uri to access the data, then obtain the ContentResolver interface through the application, obtain the data collection Cursor object through the interface, and finally obtain the final required data through the Cursor object search index. Okay, this is all about this chapter. Due to my limited technology, the article will inevitably have errors, and I hope to criticize and correct me. Later, I will find time to write a source code analysis of the working process of the four major components of Android. Thank you for your reading.

The above is a detailed explanation of Android application components. For more information about Android application components, please follow my other related articles!