Tutorials  /  JavaScript

Effective Event Handling in Android with BroadcastReceivers

Ccentron Redaktion · June 2024 ·3 min read ·JavaScript, Tutorial

Android offers a variety of components that enable the development of robust and versatile applications. One of these important components is the BroadcastReceiver. Today, we'll take a closer look at the Android BroadcastReceiver and demonstrate how it's implemented in an Android application.

What is an Android BroadcastReceiver?

The Android BroadcastReceiver is a dormant component of the Android operating system that listens for system-wide broadcast events or Intents. When one of these events occurs, the application is activated by either creating a status bar notification or performing a task. Unlike activities, the Android BroadcastReceiver does not contain a user interface. It's typically implemented to delegate tasks to services depending on the type of Intent data received.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Key System-Wide Generated Intents

Some key system-wide generated intents are:

  • android.intent.action.BATTERY_LOW: Indicates that the device is in low battery state.
  • android.intent.action.BOOT_COMPLETED: This is broadcast once after the system has finished booting.
  • android.intent.action.CALL: To make a call to a specific person.
  • android.intent.action.DATE_CHANGED: The date has changed.
  • android.intent.action.REBOOT: Reboot the device.
  • android.net.conn.CONNECTIVITY_CHANGE: The mobile network or Wi-Fi connection has changed (or reset).

Implementing a BroadcastReceiver in Android

To set up a BroadcastReceiver in an Android application, two steps need to be performed:

  1. Creating a BroadcastReceiver
  2. Registering a BroadcastReceiver

Creating a BroadcastReceiver

A custom BroadcastReceiver can be implemented as follows:

Code
public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }
 
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_SHORT).show();
    }
}

The BroadcastReceiver is an abstract class, whose method onReceive() is abstract. This method is called first on the registered broadcast receivers when an event occurs. The Intent object is passed along with any additional data. A Context object is also available and used to start an activity or service.

Registering the BroadcastReceiver in the Android App

A BroadcastReceiver can be registered in two ways:

  1. By defining in the AndroidManifest.xml file
Code
 <receiver android:name=".ConnectionReceiver" >
    <intent-filter >
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" / >
    </intent-filter >
</receiver >
  1. Programmatically
Code
IntentFilter filter = new IntentFilter();
filter.addAction(getPackageName() + "android.net.conn.CONNECTIVITY_CHANGE");

MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver, filter);

Sending Broadcast Intents from the Activity

Code
Intent intent = new Intent();
intent.setAction("com.journaldev.CUSTOM_INTENT");
sendBroadcast(intent);

Summary

The BroadcastReceiver is an important component of Android that allows responding to system-wide events. By creating and registering BroadcastReceivers, Android applications can effectively respond to various events and perform appropriate actions.

That was a brief introduction and tutorial on the Android BroadcastReceiver. Hopefully, this post has helped you better understand the concept and how it can be applied in your own Android development.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren