How to Use LocalBroadcastManager in Android?

The local broadcast manager is one of the most important features of Android applications. The broadcast manager will notify us when a specific event occurs. Broadcast manager which notifies users with a message of mobile application. In this article, we will take a look at one example of broadcast.

How to use the broadcast manager in Android?

To use broadcast manager in our android app, two processes of broadcast  first is registering broadcast and receiving update.

  •  registering a broadcast

       For performing a specific action using broadcast manager, first register board cast.

  • Receiving the updates of broadcast

       Receiving the updates  on broadcast using onReceive() method .


Step 1: Create a new project in Android  or you can use an already created project.  


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

 implement the same invoke the following code inside activity_main.xml file. 

 <?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/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="80dp"
android:text="Candidroot solutions"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/txtTitlemain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="124dp"
android:padding="10dp"
android:text="Welcome"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />

<Button
android:id="@+id/btnSendBoradcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtTitlemain"
android:layout_centerInParent="true"
android:layout_marginTop="120dp"
android:text="Send BroadCast"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitlemain" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

Step 3: Create an activity_main.java file in your package folder .

 Implement the same invoke the following code inside activity_main.java file.

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

public class MainActivity extends AppCompatActivity {
private TextView txtTitle;
private Button btnSendBoradcast;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_two);
txtTitle = findViewById(R.id.txtTitlemain);
btnSendBoradcast = findViewById(R.id.btnSendBoradcast);
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter("custom-action-local-broadcast"));
btnSendBoradcast.setOnClickListener(v -> {

Intent intent = new Intent("custom-action-local-broadcast");

intent.putExtra("message", "Welcome \n to \n CandidRoot Solutions ");

LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(intent);
});

}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
txtTitle.setText(message);
}
};

}

 

Step 4: Output of above example .


Happy coding!

365Bloggy May 31, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive