Getting to Know Android App Structures: Activities, Fragments, and Broadcast Receivers

2. Responding to Network Connectivity Changes:

3. Responding to Incoming SMS Messages:

  • Broadcast Receiver can detect incoming SMS messages (ACTION_SMS_RECEIVED).
  • Example: Display notifications or process SMS messages automatically.

5. Service: Background Process Without UI

Service is an essential component in an Android app that allows you to run tasks in the background without a user interface. Service is ideal for tasks that need to continue running even when the app is down, such as streaming music, data syncing, or location updates.

By using Service, you can ensure that your app continues to function properly even when users aren’t interacting directly with the app.

Types of Service

Service can be divided into two main types:

1. Started Service

Started Service is a type of Service that is started by another component, such as Activity or Broadcast Receiver, and will continue to run until the task is completed.

How It Works:

  • Started Service starts by calling the startService() method.
  • Service will run in the background until the task is completed or stopped manually.
  • Once the task is complete, the Service will stop automatically.

Example code:

Intent intent =  new Intent(this,  MyStartedService.class);
startService(intent);

2. Bound Service

Bound Service is a type of Service that binds to other components, such as Activity or Fragment, and will run as long as those components are active.

How It Works:

  • Bound Service starts by calling the bindService() method.
  • Service will run as long as there are components attached to it.
  • If all components release the bond, the Service will stop automatically.

Example code:

Intent intent = new Intent(this,  MyBoundService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

The Difference Between Started Service and Bound Service

AspectsStarted ServiceBound Service
How to Get StartedStarting with startService().Starting with bindService().
DurationRuns until the task is completed or stopped.Runs as long as there are components attached.
CommunicationUnable to communicate directly with other components.Can communicate directly with bonded components.
Usage ExamplesMusic streaming, data syncing.Music player, real-time communication.

Examples of Service Usage in Applications

1. Music Apps:

  • Started Service: Play music in the background when the app is inactive.
  • Bound Service: Control the playback of music from Activity or Fragment.

2. Sports Apps:

  • Started Service: Tracks the user’s location while running or cycling.
  • Bound Service: Displays real-time location and speed data on the Activity.

3. Chat App:

  • Started Service: Send and receive messages in the background.
  • Bound Service: Displays new messages in real-time on Activity.

6. Content Provider: Data Sharing Between Applications

Content Provider is an essential component in Android apps that allows apps to share data with other apps. By using Content Provider, you can give access to your app’s data, such as contact lists, media files, or databases, to other apps.

Content Provider also allows your app to access data from other apps, such as accessing a user’s contact list or photo gallery.

Latest Articles