SoFunction
Updated on 2025-04-10

Android's four major components: Activity/Service/Broadcast/ContentProvider function example

Android is a Linux-based operating system with free and open source code. Android is divided into four layers, from the high-level to the bottom layer, namely the application layer, the application framework layer, the system runtime library layer and the Linux kernel layer. Android has four basic components: Activity, Service Service, BroadcastReceiver broadcast receiver, and Content Provider content provider.

1. Activity function

Activity is the carrier of the Android application interface, responsible for displaying and responding to user operations, and can usually be regarded as an interface, view or screen. During the entire life cycle of Android applications, Activity acts as a bridge for the interaction between the foreground interface and the backend logic. At the same time, the Activity can simultaneously launch or associate another Activity instance, thus forming the interaction of multiple Activity in Android applications.

As one of the four major components of Android, Activity has the following characteristics:
1. Activity is a single UI advanced element, usually composed of multiple views.
2. Activity has a life cycle, including visible, invisible, activity, background and destruction.
3. Activity can cooperate or run with other activities, thus forming a complex interface and complex operation of Android applications.

Here is an example of a simple Activity:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_main);
    }
}

2. Service function

In Android applications, Service is a backend running component, responsible for performing some operations that do not require user interaction, such as playing music, downloading files, etc. The Service component can run in the background for a long time, and the Service can continue to run even if the user exits the application. Service components can also be started or stopped by other components, thereby enabling interaction between different components.

As one of the four major components of Android, Service has the following characteristics:
1. Service is a background operation component that runs in the process of the application.
2. Service can handle long-term running operations, such as music playback, file download, etc., and will not be stopped when the user exits the application.
3. Service can be started, bound or stopped by other components or applications.

Here is an example of a simple Service:

public class MyService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Write long-term service operation code here        return (intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

3. Broadcast Receiver function

Broadcast Receiver is a message receiver in the Android system, which is responsible for receiving messages sent by the system and applications, and triggering related operations after receiving the message. For example, when receiving a text message or a network state change, the Broadcast Receiver may be responsible for receiving these messages and performing corresponding operations, such as popping up a prompt box, issuing a notification, starting other components, etc.

As one of the four major components of Android, Broadcast Receiver has the following characteristics:
1. The recipients of all messages in the Android system are Broadcast Receiver components, such as system broadcast, custom broadcast, etc.
2. Broadcast Receiver can communicate and interact between applications and systems.
3. Broadcast Receiver can be registered and deregistered dynamically or statically by other components or applications.

Here is an example of a simple Broadcast Receiver:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Write the operation after receiving messages here    }
}

4. The function of Content Provider

Content Provider is a component of data storage and data sharing in the Android system. It is used to store, retrieve, modify and delete data in applications, and can also share and interact with data with other applications or system data. For example, contacts, text messages, music information, etc. are all read, store and share data through Content Provider.

As one of the four major components of Android, Content Provider has the following features:
1. Content Provider component is used to store and share data.
2. Accessing data in Content Provider requires access through a unified URI address. Without a unified URI address, data access cannot be performed.
3. Content Provider can share and interact with other applications or system data.

Here is an example of a simple Content Provider:

public class MyContentProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
        // handle the initialization work of Content Provider here        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        // This handles data query operations in Content Provider        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}

5. Summary

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

This is the article about the four major components of Android: Activity/Service/Broadcast/ContentProvider. For more information about the four major components of Android, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!