Android apps can be written using Kotlin, Java, and C++ languages. The Android SDK tools compile your code and generate Android package (APK). APK is an archive file with an .apk suffix. App components are the building blocks of an Android application. Each component is an entry point through which the system or a user can enter your app. Different types of app components are

  • Activities
  • Services
  • Broadcast receivers
  • Content providers

Above component must be defined in the project manifest file. When the system starts a component, it starts the process for that app. To activate a component in another app, deliver a message to the system that specifies your intent to start a particular component. The system then activates the component for you.

Activities

An activity is the entry point for interacting with the user. It represents a single screen with a user interface. An App can have multiple activities. You implement an activity as a subclass of the Activity class.

public class MainActivity extends Activity { 

}

Below example illustrate how to declare MainAcivity in manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:icon="@drawable/app_icon.png" ... >
        <activity android:name=".MainActivity"
                  android:label="@string/example_label" ... >
        </activity>
        ...
    </application>
</manifest>

Services

A service is a component that runs in the background and does not provide a user interface. It can perform long-running operations or to perform work for remote processes. For example, a service might fetch data over the network without blocking user interaction with an activity. A service can be started an activity. If you start a service that runs from an activity and the user navigates away from the activity to another app, the service will continue to run.

A regular background service is not something the user is directly aware as running. Bound services provide an API to another process. If process A is bound to a service in process B, it knows that it needs to keep process B (and its service) running for A.

A service is implemented as a subclass of Service.

public class MyService extends Services { 
  //code 
}

Below example illustrate how to declare MyService in manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest...>
    <application android:icon="@drawable/app_icon.png" ... >
        ...
       <service android:name=".MyService" />
    </application>
</manifest>

Broadcast Receivers

A broadcast receiver is a component that enables the system to deliver events to the app outside of a regular user flow. It allow the app to respond to system-wide broadcast announcements. System can deliver broadcasts even to apps that aren’t currently running. For example, an app can schedule an alarm to post a notification to tell the user about an upcoming event. Apps can also initiate broadcasts. Broadcast receivers don’t display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. It is intended to do a very minimal amount of work.

A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object.

public class MyBroadcast extends BroadcastReceiver {
  //code
}

Below example illustrate how to receive the battery low intent.

<?xml version="1.0" encoding="utf-8"?>
<manifest...>
    <application android:icon="@drawable/app_icon.png" ... >
        ...    
      <receiver android:name=".YourReceiver">
        <intent-filter>
          <action android:name="android.intent.action.BATTERY_LOW" />
        </intent-filter>
      </receiver>
    </application>
</manifest> 

Content Providers

Content Provider is a component that allows applications to share data among multiple applications. A content provider manages a shared set of app data. It allows other apps to query or modify the data. To the system, a content provider is an entry point into an app for publishing named data items, identified by a URI scheme. Content providers are also useful for reading and writing data that is private to your app and not shared.

A content provider is implemented as a subclass of ContentProvider.

public class MyProvider extends ContentProvider {
  //code
}

Below example illustrate how to specify content provider in manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest...>
    <application android:icon="@drawable/app_icon.png" ... >
        ...
      <provider android:name=".MyProvider"

      </provider>
    </application>
</manifest>

Activating Components

Activities, services, and broadcast receivers are activated by an asynchronous message called an intent. Intents bind individual components to each other at runtime. An intent is created with an Intent object, which defines a message to activate either a specific component (explicit intent) or a specific type of component (implicit intent).

For activities and services, an intent defines the action to perform (for example, to view or send something) and may specify the URI of the data to act on, among other things that the component being started might need to know. For broadcast receivers, the intent simply defines the announcement being broadcast.

Content providers are activated when targeted by a request from a ContentResolver. The content resolver handles all direct transactions with the content provider so that the component that’s performing transactions with the provider doesn’t need to and instead calls methods on the ContentResolver object.