Explain the four application components of Android in detail

  • 2021-12-11 08:58:19
  • OfStack

One of the core features of Android is that one application can be used as an element in other applications and can provide data for other applications. For example, if a program needs to use some controls to load some pictures, and another program has developed this function and can be used by other programs, it can directly use cross-process communication to call the function of that program instead of developing another one by itself. To achieve this functionality, the Android system must be able to start its process when any 1 part of the application is required and instantiate the Java object for that part. Therefore, unlike most programs in other systems, Android programs do not have only a single entry point, but they have the necessary components for system instantiation and operation, and Android provides four major components; In addition to BroadcastReceiver, Activity, Service and ContentProvider must be registered in AndroidManifest. xml, while BroadcastReceiver can be registered in AndroidManifest. xml file, Java code or kotlin code. After Android 8.0, the static registration broadcast reception failure in AndroidManifest. xml file is due to the official optimization of power consumption to avoid APP abusing a processing method of broadcast.

1. Activity

Activty is a display component, and Activity provides a visual user interface for users. For example, a calling program might have an Activity to display contacts who can make calls, The second Activity is used to create new contacts to write information, and the other Activity is used to view specific contacts or change contact information. Although the user interface provided by each Activity in the application program is highly aggregated, each Activity is independent of other Activity, and each instantiated Activity is a subclass of Activity. Intent can trigger the start of Activity, and Intent can be divided into explicit Intent trigger and implicit Intent trigger. An explicit Intent trigger can be explicitly pointed to the Activity component and is represented by the following code:


 Intent in = new Intent(this,SecondActivity.class)
 MainActivity.this.startActivity(in)

An implicit Intent trigger is a target component that points to one or more Activity, or it may not have a target Activity. Its implicit trigger is represented by the following code:


Intent intent = new Intent();
intent.setPackage("com.xe.launchmode");
intent.setAction("com.xe.actoin.MAP");
intent.addCategory("android.intent.category.APP_MAPS");
MainActivity.this.startActivity(intent);

2. Service

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


Intent in = new Intent(this,SecondActivity.class)
startActivity(in)

However, there are two kinds of Service. When it is in the startup state, it can do some background tasks without interacting with the user interface. Its life cycle is as long as that of the application program. Playing music by multimedia player is a very good example of applying Service. The multimedia player program may contain one or more Activity through which the user selects and plays music. However, music playback does not require an Activity to process, because users may want music 1 live broadcast to be put down, even if they quit the player to execute other applications. In order for Music 1 to be played live, the multimedia player Activity may start an Service to play music in the background. The Android system causes the music playback Service1 to run straight, even after the Activity that started the Service exits. Its startup can be represented by the following code:


Intent in = new Intent(this,SecondActivity.class)
MainActivity.this.startService(in)

When it is bound, it can do some background tasks and interact with the user interface. Its life cycle is as long as that of 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, MyService.class); 
MainActivity.this.bindService(intent,new ServiceConnection(),Context.BIND_AUTO_CREATE);

Open in the above 2, no matter which one, you can't directly do time-consuming operation in Service, because it runs in the main thread. If you have to do time-consuming operation, you should open a worker thread to execute it.

3. BroadcastReceiver

1 generally does not perform any tasks, but only receives and broadcasts notifications to Class 1 components accordingly. Most broadcast notifications are generated by the system, such as changing the time zone, alerting the alarm clock, selecting a picture by the user, or changing the language preference by the user. Applications can also send broadcast notifications, such as notifying other applications that some data has been downloaded to the device and can be used; 1 application's BroadcastReceiver to respond to its notifications, and all BroadcastReceiver implementation classes extend 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 two registration methods are introduced. The first is static registration, which is completed in the AndroidManifest. xml file. When installing the application, it will be parsed by the application, and the broadcast can be received without starting the application. It is represented by the following code for monitoring the state change of wifi:


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

From the above code, we can find that the matching of the receiving process is through < intent-filter > To describe, it can be concluded that broadcasting is a low-coupling observer mode.
Another way is dynamic registration, which needs to start the application before receiving the broadcast. It is registered 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();
 filter.addAction("com.xe.intent.action.ACTION_1");
 filter.addAction("com.xe.intent.action.ACTION_2");
 SecondActivity.this.registerReceiver(receiver,filter);

Sending a broadcast can be achieved by the following code:


Intent intent = new Intent();
intent.setAction("com.xe.intent.action.ACTION_2");
MainActivity.this.sendBroadcast(intent);

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

4. ContentProvider

ContentProvider is a shared data component, through which applications can access the data of other applications, including private data of other applications; Like Service1, 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 whether the host process of the component processing the request is already running, and if not, start the process immediately. ContentProvider provides an external interface ContentResolver for other processes to access data. The following 1 part of the code simply represents the use process of query method:


Uri bookUri = Uri.parse("content://com.zyb.provider/data");
ContentResolver cr = ContentProviderActivity.this.getContentResolver();
Cursor bookCursor = cr.query(bookUri,new String[]{"_id","name"},null,null,null);
while (bookCursor.moveToNext()) {
 int id = bookCursor.getInt(0);
 String name = bookCursor.getString(1);
}

In the above code, we first create an Uri to access the data, then obtain the ContentResolver interface through the application program, obtain the data collection Cursor object through this interface, and finally obtain the final required data through the Cursor object lookup index. Well, the content of this chapter is written here. Because of my limited technology, the article will inevitably make mistakes, and I hope to criticize and correct it; Later, I will find a time to write 1 under the Android4 components working process of the source code analysis, thank you for reading.

These are the details of Android application components. For more information about Android application components, please pay attention to other related articles on this site!


Related articles: