× back

Android - Application Components

In Android, an application is composed of several key components that work together to create a functional and interactive app. These components manage different aspects of the application’s behavior and interaction with users and other apps.

1: Activities

Activities represent a single screen with a user interface. They are crucial for interacting with users and are often considered the entry points of an application.

  • Purpose: Display UI elements and handle user interactions.
  • Example: A settings screen or a login page.
  • Lifecycle: Activities go through various states such as created, started, resumed, paused, stopped, and destroyed.

2: Services

Services run in the background to perform long-running operations or tasks without user interaction. They continue to run even if the user switches to another app.

  • Purpose: Handle tasks like playing music, performing network operations, or handling system-level functions.
  • Example: A music player that continues playing audio in the background.
  • Types: Foreground services (display notifications to keep the user informed) and background services (perform tasks without user visibility).

3: Broadcast Receivers

Broadcast receivers listen for and respond to system-wide broadcast announcements or events. They handle events such as incoming calls or system changes.

  • Purpose: React to system-wide events or messages, such as network connectivity changes or battery low warnings.
  • Example: A receiver that listens for changes in network connectivity and updates the app’s UI accordingly.
  • Usage: Implemented by extending the BroadcastReceiver class and registering intents in the manifest or at runtime.

4: Content Providers

Content providers manage and share app data with other applications. They provide a standard interface for data access and manipulation across different apps.

  • Purpose: Facilitate data sharing and querying between different apps and manage data storage.
  • Example: Contacts provider that allows apps to access and modify the user’s contact information.
  • Usage: Implemented by extending the ContentProvider class and defining URIs for accessing data.

Additional Components

Besides the main components, there are other essential elements in Android applications that enhance functionality and user experience.

  • Views: Basic building blocks for UI elements like buttons, text fields, and images. They are used to display content and handle user interactions.
  • Fragments: Reusable portions of an activity’s UI that can be combined to create a flexible and dynamic user interface.
  • Layouts: Define the structure and arrangement of UI elements on the screen. Examples include LinearLayout and RelativeLayout.
  • Intents: Messages used to request actions from other components or apps. They can be used to start activities, services, or deliver broadcasts.
  • Resources: Assets such as strings, images, and layouts that are used by the application. They are defined in XML files in the res directory.
  • Manifest: The AndroidManifest.xml file contains essential information about the app, such as its components, permissions, and configuration details.

Android Activity

An Activity in Android is a crucial component that provides the window in which an app draws its user interface (UI). Each activity represents a single screen in an Android application, much like a window or frame in Java. The activity window typically takes up the full screen but can be resized or float over other windows in certain scenarios.

Activities allow developers to place all UI components or widgets in a single screen. Generally, each activity corresponds to one screen of the app. For example, one activity may handle a Preferences screen, while another may manage a Select Photo screen.

The Android Activity class is a subclass of ContextThemeWrapper, meaning it inherits the ability to apply themes and styles to its UI components.

Key Characteristics of an Activity:

Android Activity Lifecycle

The Android Activity Lifecycle refers to the various states that an activity can exist in as it moves between foreground and background or when it gets interrupted by another activity. These states define how an activity behaves and manages its resources. There are four main states:

The activity lifecycle is crucial for managing an Android app's resources efficiently. Understanding these states helps in designing applications that perform well under different conditions, such as interruptions or multitasking. Activities provide the foundation for user interaction by managing the screen, handling user inputs, and maintaining their states as they transition through various lifecycle stages.

Android Toast

A Toast is a small message that pops up on the screen for a short duration, providing feedback to the user. Unlike a dialog, a toast does not block user interaction with the app, making it useful for showing non-intrusive messages like "Task completed," "Network error," or "File saved successfully."

Why Use a Toast?

Basic Syntax of Toast

In Android, a Toast message can be created and displayed using the following syntax:

                
Toast.makeText(context, message, duration).show();
                
            

Let’s break this down:

Arguments Passed to a Toast

Example of a Basic Toast

        
Toast.makeText(this, "Hello, World!", Toast.LENGTH_SHORT).show();
        
    

Step-by-Step Explanation:

  1. this: Refers to the current activity context.
  2. "Hello, World!": This is the message that will be displayed on the screen.
  3. Toast.LENGTH_SHORT: The duration for which the toast will be shown (approximately 2 seconds).
  4. .show(): This method ensures that the toast is actually displayed to the user.

Simple program in which when we click on a button a Toast message is displayed.

                            
package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private int i = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        Button myBtn = findViewById(R.id.button);
        myBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                i = i + 1;
                Toast.makeText(MainActivity.this, "No. of times button is clicked = " + i , Toast.LENGTH_SHORT).show();
            }
        });
    }

}
                            
                        
                            
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toast Demo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.486"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.091" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#5DE063"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.584" />

</androidx.constraintlayout.widget.ConstraintLayout>
                            
                        

Android Activity Lifecycle - Sample Program

We already know that the Android Activity Lifecycle describes the different states an activity goes through from its creation to its destruction. Below is a sample Android program that uses Toast messages to indicate when different lifecycle methods are called. This will help you understand how activities transition through these states.

In this example, we'll implement a simple Android activity that displays a Toast message for each lifecycle method:

Activity Lifecycle Program Code

                
package com.example.lifecycleexample;

import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, "Activity Created", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Toast.makeText(this, "Activity Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this, "Activity Resumed", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Toast.makeText(this, "Activity Paused", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Toast.makeText(this, "Activity Stopped", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Activity Destroyed", Toast.LENGTH_SHORT).show();
    }
}
                
            

Explanation of the Program

This program demonstrates how to use Toast messages to track the lifecycle of an Android activity. Here’s what each lifecycle method does:

Example Scenario:

What is a Service in Android?

A Service in Android is a component that runs in the background to perform long-running operations without needing to interact with the user. It doesn't provide a user interface but works to:

Types of Services:

Why Use a Service?

Services are helpful for running tasks in the background, such as playing music, downloading files, or handling long-running computations.

Why Do We Need Intents for Services?

n Android, Intents are messaging objects used to communicate between different app components. When it comes to Services, Intents are essential because:

Intent

An Intent is a messaging object that allows you to request an action from another component, such as launching an Activity, starting a Service, or delivering a broadcast. Intents can be used to communicate between different components of an application or even between applications.

  • Types of Intents:
    1. Explicit Intent:

      An Explicit Intent directly specifies the component (usually an Activity or Service) that you want to start. This is useful when you know the class name of the component you want to interact with.

      Example: Starting a new Activity within the same app.

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

      In this example, MainActivity.this is the context, and SecondActivity.class is the class of the Activity we want to start.

    2. Implicit Intent:

      An Implicit Intent does not specify the component to be started. Instead, it defines an action to be performed, allowing any app that can handle that action to respond. This is useful for actions that could be handled by multiple applications.

      Example: Sharing content across apps.

                                          
      Intent shareIntent = new Intent(Intent.ACTION_SEND);
      shareIntent.setType("text/plain");
      shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, check this out!");
      startActivity(Intent.createChooser(shareIntent, "Share via"));
                                          
                                      

      In this example, Intent.ACTION_SEND indicates that we want to share some text. The setType() method specifies the type of data being shared, and putExtra() adds the actual text content. The Intent.createChooser() method presents a chooser dialog to allow the user to select an app to share with.

Understanding the Intent Class:

The Intent class in Android is used to perform operations such as starting another Activity or Service. Here’s how it works:

Creating an Intent:

To create an Intent, you typically use the following constructor:

                    
Intent(Context context, Class<?> cls)
                    
                
  • Context context: This is the current context of your application. It provides access to application-specific resources and classes. In most cases, you will pass the current Activity (e.g., MainActivity.this).
  • Class<?> cls: This parameter is the class of the component you want to start. For example, if you want to start SecondActivity, you would pass SecondActivity.class.

Example Usage of Intent:

Now that we know how to create an Intent, let’s look at an example where we use it to navigate between two activities: MainActivity and SecondActivity.

            
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Find the button in the layout
        Button button = findViewById(R.id.button);
        // Set an OnClickListener for the button
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Create an Explicit Intent to open SecondActivity
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);  // Start SecondActivity
            }
        });
    }
}
            
        
            
package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        // Find the button in the layout
        Button secBtn = findViewById(R.id.button2);
        // Set an OnClickListener for the button
        secBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Create an Explicit Intent to return to MainActivity
                Intent intent = new Intent(SecondActivity.this, MainActivity.class);
                startActivity(intent);  // Start MainActivity
                finish();  // Close SecondActivity
            }
        });
    }
}
            
        

Explanation of the Example:

  • The MainActivity contains a button that, when clicked, starts the SecondActivity using an Explicit Intent.
  • The SecondActivity also contains a button that, when clicked, returns the user to the MainActivity.

Method Breakdown:

  • onCreate(Bundle savedInstanceState): Called when the activity is first created. Initializes the activity and sets the layout using setContentView().
  • findViewById(int id): Retrieves a view from the layout using its ID.
  • setOnClickListener(View.OnClickListener listener): Sets an action for the button click. Executes the strong inside onClick() when the button is clicked.
  • new Intent(Context context, Class cls): Creates a new intent to start an activity. The first parameter is the context (current activity), and the second is the activity class to open.
  • startActivity(Intent intent): Starts the activity defined in the intent. It opens the specified activity.
  • finish(): Closes the current activity and removes it from the back stack, preventing the user from returning to it by pressing the back button.

Android Service Lifecycle

In Android, a Service is a component that runs in the background to perform long-running operations or to perform work for remote processes. Unlike Activities, Services do not have a user interface and can continue running even if the user switches to another application. Understanding the lifecycle of a Service is crucial for managing its operations and resources effectively.

Key Lifecycle Methods

  1. onCreate()

    Called when the service is first created. This is where you perform one-time initialization tasks, such as setting up resources.

                                
    @Override
    public void onCreate() {
        super.onCreate();
        // Initialization code, e.g., setting up media player
    }
                                
                            
  2. onStartCommand()

    Called every time a client starts the service using startService(). This method allows the service to perform its operations.

    It returns an integer that indicates how the system should handle the service if it is killed:

    • START_NOT_STICKY: The service will not be recreated until an explicit request is made.
    • START_STICKY: The service will be recreated, but the last intent will not be delivered.
    • START_REDELIVER_INTENT: The service will be recreated, and the last intent will be redelivered.
                                
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Code to execute when the service is started
        return START_STICKY; // or START_NOT_STICKY, etc.
    }
                                
                            
  3. onBind()

    Called when a client binds to the service using bindService(). This method is used for bound services, allowing clients to interact with the service.

    It must return an IBinder object. If the service is not intended to be bound, it should return null.

                                
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null; // No binding provided
    }
                                
                            
  4. onUnbind()

    Called when all clients have disconnected from a particular interface published by the service. This can be used to perform cleanup if needed.

                                
    @Override
    public boolean onUnbind(Intent intent) {
        // Code to execute when service is unbound
        return super.onUnbind(intent);
    }
                                
                            
  5. onDestroy()

    Called when the service is no longer used and is being destroyed. This is where you should clean up resources, such as stopping threads or releasing resources.

                                
    @Override
    public void onDestroy() {
        super.onDestroy();
        // Cleanup code, e.g., stopping media player
    }
                            
                        

Summary of the Service Lifecycle

  • Creation: onCreate() is called once when the service is created.
  • Starting: onStartCommand() is called every time the service is started.
  • Binding: onBind() is called when a client binds to the service; return null if not needed.
  • Unbinding: onUnbind() is called when clients unbind from the service.
  • Destruction: onDestroy() is called when the service is stopped and needs to release resources.

What is a Started Service?

  • A started service is a type of service that is explicitly started by a component (usually an Activity or BroadcastReceiver) and continues running in the background until it completes its task or is explicitly stopped. This type of service is used for tasks that don't require direct interaction with the service after it starts, like downloading a file or playing audio.

Creating a Started Service Example: Playing a Default Ringtone

Now that you understand Intents and how they work, let's move on to an example of using an Intent to start a Service. In this case, we will create a Service to play a default ringtone.

Step 1: Create the Service Class

  • In this example, we will use the MediaPlayer class to play a ringtone in the background. The Service will be started and stopped using Intents.
                            
package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class MyService extends Service {

    private MediaPlayer mPlayer;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null; // We don't need to bind this service
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mPlayer = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI); // Play default ringtone
        mPlayer.setLooping(true); // Loop the ringtone
        mPlayer.start(); // Start playing
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent, flags, startId); // Call super method
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPlayer != null) {
            mPlayer.stop(); // Stop playing
            mPlayer.release(); // Release resources
        }
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
    }
}
                            
                        

Explanation:

  • The MyService class extends the Service class.
  • MediaPlayer: This class is used to control playback of audio files. We will use it to play the default ringtone.
  • onCreate(): This method is called when the service is first created. Here, we show a toast message indicating that the service has been created.
  • onStartCommand(): This method is called every time the service is started. We create a MediaPlayer instance to play the default ringtone, set it to loop, and start the playback. We also display a toast message indicating that the service has started.
  • onDestroy(): This method is called when the service is being destroyed. We stop the MediaPlayer and release its resources to prevent memory leaks.

Step 2: Start and Stop the Service from an Activity

  • We’ll use Intents to start and stop the service from MainActivity.
                            
package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private Button startButton, stopButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startButton = findViewById(R.id.startButton);
        stopButton = findViewById(R.id.stopButton);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent serviceIntent = new Intent(MainActivity.this, MyService.class);
                startService(serviceIntent); // Start the service
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent serviceIntent = new Intent(MainActivity.this, MyService.class);
                stopService(serviceIntent); // Stop the service
            }
        });
    }
}
                            
                        

Explanation:

  • The MainActivity class is where we manage our user interface.
  • We define two buttons: one to start the service and another to stop it.
  • startButton: When clicked, it creates an Explicit Intent for MyService and calls startService() to start the service.
  • stopButton: When clicked, it creates an Explicit Intent for MyService and calls stopService() to stop the service.

Step 3: Update the Layout

  • The layout needs two buttons: one to start the service and one to stop it.
                            
<?xml version="1.0" encoding="utf-8"?>
                            <androidx.constraintlayout.widget.ConstraintLayout
                            xmlns:android="http://schemas.android.com/apk/res/android"
                            xmlns:app="http://schemas.android.com/apk/res-auto"
                            xmlns:tools="http://schemas.android.com/tools"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            tools:context=".MainActivity">

                            <Button
                            android:id="@+id/startServiceBtn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Start Service" />

                            <Button
                            android:id="@+id/stopServiceBtn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Stop Service"
                            android:layout_below="@id/startServiceBtn"
                            android:layout_marginTop="20dp" />
                            </androidx.constraintlayout.widget.ConstraintLayout>
                            
                            

Explanation:

  • We create a LinearLayout with two buttons: startButton and stopButton.
  • These buttons will allow the user to control the ringtone playback.

Step 4: Declare the Service in AndroidManifest.xml

  • Ensure that the Service is declared in the app’s manifest.
                    
<service android:name=".MyRingtoneService" />
                    
                

Explanation:

  • In the AndroidManifest.xml, we declare our service using <service android:name=".MyService" />.
  • This allows the Android system to know about our Service, enabling it to be started and stopped as needed.

Summary of the Program:

  • We created a Service called MyService that plays a default ringtone using the MediaPlayer class.
  • The service can be started and stopped using buttons in the MainActivity, which uses Intents to communicate with the Service.
  • We also ensured that the service is properly declared in the AndroidManifest.xml to allow the Android system to manage it.
  • This example demonstrates how to create a Service, manage audio playback, and use Intents to control the Service from an Activity, providing a practical understanding of service management in Android.

What is Bound Services in Android

  • A Bound Service is one that provides a client-server interface to allow components (like an Activity) to bind, communicate, and perform tasks. When an Activity binds to a service, the service provides an interface for the activity to interact with it. The service runs only as long as at least one client is bound to it.
  • Key Concepts of Bound Service:
    • Binding: When a component (like an Activity) binds to the service, it uses a ServiceConnection object.
    • Unbinding: Once the component no longer needs to interact with the service, it can unbind, and the service may stop running if no other component is bound to it.

Step-by-Step Explanation of a Bound service

  1. Service Class:
    • Create a service class by extending the Service class.
    • Override the onBind() method to return an instance of IBinder. This is how the client (Activity) interacts with the service.
  2. Binding the Service:
    • You need to create a ServiceConnection to manage the connection between the Activity and the service. This includes methods like onServiceConnected() and onServiceDisconnected().
  3. LocalBinder:
    • The service provides a Binder object through which the client can interact with the service. This is often done by creating an inner class that extends Binder.
  4. Unbinding the Service:
    • When the client no longer needs the service, it can call unbindService() to release the connection.

Example: Bound Service for Generating Random Numbers

                                
package com.example.mybcaboundservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import androidx.annotation.Nullable;

import java.util.Random;

public class BoundedService extends Service {

    private Random noGenerator = new Random();
    public BoundedService(){}

    // IBinder object to interact with the service
    private IBinder myBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        BoundedService getService(){
            return BoundedService.this;
        }
    }

    // This is where the client (Activity) binds to the service
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    // A method that returns a random number
    public int getRandom(){
        return noGenerator.nextInt(100);
    }
}
                                
                            
                                
package com.example.mybcaboundservice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    BoundedService bService;  // Reference to the bound service
    Intent myIntent;  // Intent for binding to the service

    boolean bound = false;  // Tracks if the service is bound

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void start(View view){
        // Binding the service
        myIntent = new Intent(MainActivity.this, BoundedService.class);
        bindService(myIntent, sConnection, BIND_AUTO_CREATE);  // Automatically create and bind service
        Toast.makeText(MainActivity.this, "Service Bounded", Toast.LENGTH_SHORT).show();
    }

    // ServiceConnection object that manages the connection with the service
    private ServiceConnection sConnection= new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
           BoundedService.LocalBinder myBinder = (BoundedService.LocalBinder) iBinder;
            bService = myBinder.getService();  // Get service instance
            bound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            bound = false;
        }
    };

    public void stop(View view){
        // Unbind the service
        unbindService(sConnection);
        Toast.makeText(MainActivity.this, "Service UnBounded", Toast.LENGTH_SHORT).show();
        bound = false;
    }

    public void displayNumber(View view){
        if(bound){
            // Use the bound service to generate a random number
            int i = bService.getRandom();
            Toast.makeText(MainActivity.this, "" + i, Toast.LENGTH_SHORT).show();
        }
    }
}
                                
                            

In this example:

  • We start the service using bindService() and unbind it using unbindService().
  • The BoundedService class generates a random number and returns it to the Activity when requested.

Adapters in Android

Understanding ListView

Example: Using ArrayAdapter to Display a List of Strings

Step 1: Add ListView in XML Layout

Step 2: Create the Activity Class

            
package com.example.mybcaadapter;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    ListView myListView;
    String myArr[] = {"GEHU-BHIMTAL", "GEHU-HLD", "GEHU-DDN"};  // Array of strings

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Reference the ListView from the layout
        myListView = findViewById(R.id.myListXML);

        // Create an ArrayAdapter to bind the data to the ListView
        ArrayAdapter myAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myArr);
        myListView.setAdapter(myAdapter);  // Set the adapter for the ListView
    }
}
            
        

Explanation of the Code:

This example demonstrates how to create a ListView in Android, bind data using an ArrayAdapter, and display a simple list of items.