Android Local Broadcast Receiver

Simple Local broadcast receiver example

MainActivity

package com.example.eg.localbroadcastreciver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        // adding click event for the button
        findViewById(R.id.send_broadcast).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //the following code will be inside your service
                //creating intent MyTaskStatus
                Intent myIntent = new Intent("MyTaskStatus");
                myIntent.putExtra("MyMessage", "Hi");

                // Send broadcast
                LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(myIntent);

            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();

        //register broadcast receiver for the intent MyTaskStatus
        LocalBroadcastManager.getInstance(this).registerReceiver(MyReceiver, new IntentFilter("MyTaskStatus"));

    }

    //Defining broadcast receiver
    private BroadcastReceiver MyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String message = intent.getStringExtra("MyMessage");

            Toast.makeText(MainActivity.this, "Received : "+message, Toast.LENGTH_SHORT).show();
        }
    };



    @Override
    protected void onStop() {
        super.onStop();

        LocalBroadcastManager.getInstance(this).unregisterReceiver(MyReceiver);
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.eg.localbroadcastreciver.MainActivity"
    android:orientation="vertical">


    <Button
        android:id="@+id/send_broadcast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Broadcast"/>


</LinearLayout>

 

————————————————————————————————————————————————————–

Another exaple

Here registering the broadcast receiver with the action

package com.example.eg.localbroadcastreciver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

    public static final String ACTION_DONE = "MyTaskService#ACTION_DONE";

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

        // adding click event for the button
        findViewById(R.id.send_broadcast).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //the following code will be inside your service
                // Create Intent to broadcast the task information.
                Intent intent = new Intent();
                intent.setAction(ACTION_DONE);
                intent.putExtra("msg", "Hi");

                // Send local broadcast, running Activities will be notified about the task.
                LocalBroadcastManager manager = LocalBroadcastManager.getInstance(Main2Activity.this);
                manager.sendBroadcast(intent);

            }
        });

    }

    // BroadcastReceiver to get information from MyTaskService about task completion.
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            //no need to check action again
            //if (intent.getAction().equals(ACTION_DONE)) {

            String msg = intent.getStringExtra("msg");

            Toast.makeText(context,"Broadcast received message : "+ msg, Toast.LENGTH_SHORT).show();
            //}
        }
    };

    @Override
    public void onStart() {
        super.onStart();

        //registering intent filter based on action
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_DONE);

        LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
        manager.registerReceiver(mReceiver, filter);
    }



    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
    }

}

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.eg.localbroadcastreciver.Main2Activity">

    <Button
        android:id="@+id/send_broadcast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Broadcast"/>

</RelativeLayout>

 

 

ref: https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

Author: bm on July 13, 2016
Category: Android