Basics
Broadcast is a Android way to send a message from one part of the program to another part of the program. Broadcasts can be used for many purposes, such as informing when something happens, or when something gets ready, or something else.
Broadcasting requires three phases: register broadcast, send broadcast, and receive broadcast.
In the simple example below, we click the button, then program waits for three second and after that sends the broadcast. We have broadcast receiver waiting for the broadcast. When it gets the broadcast it will show it on the screen.
Layout file
First we need to make a layout file activity_main.xml. We need one button and one textView. When clicking the button, it starts method called sendBroadcast (line 21). In the beginning the textview (line 27) has text “Waiting for the broadcast!”. That will be replaced later by the new text received by the broadcast receiver.
Copy all the code below to your own activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2"
android:onClick="sendBroadcast" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Waiting for the broadcast!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
MainActivity.java below has all the needed code.
// Receive a broadcast and send it
package com.example.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.SystemClock;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Receive broadcast
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextView textView = findViewById(R.id.text);
textView.setText("Broadcast received");
}
};
// Register broadcast
LocalBroadcastManager.getInstance(this).registerReceiver(receiver,
new IntentFilter("myBroadcast"));
}
// Send broadcast
public void sendBroadcast(View view) {
final Intent intent = new Intent("myBroadcast");
final LocalBroadcastManager broadcastManager
= LocalBroadcastManager.getInstance(this);
SystemClock.sleep(3000);
broadcastManager.sendBroadcast(intent);
}
}
Receive broadcast
In lines 24-30, we define the broadcast receiver. It will wait for the broadcast and when it receives the broadcast, it will run the onReceive() method. In this case it will write “Broadcast received” in the screen.
Register broadcast
To be able to receiver broadcasts, we need to register the broadcast receiver (lines 33-34). After registering the receiver, Android knows to send the broadcast to receiver. Different type of broadcasts are identified by name. In this case we use broadcast named “myBroadcast”. So here we define IntentFilter that will receive only broadcast with the name “myBroadcast”.
Send the broadcast
The sendBroadcast method is in lines 38-44. In line 39, we define a new intent and give it a name as an argument “myBroadcast”. In lines 40 and 41, we get a new instance of LocalBroadcastManager and give it a name localBroadcastManager. In line 42, we wait for 3 seconds (3000 milliseconds). Finally, in line 43, we send the broadcast.
Finally run the program
Now run the program. When you click the button, it starts sendBroadcast method. After 3 seconds the broadcast receiver will receive “myBroadcast” broadcast and change the text on the screen to “Broadcast received”.