0

I'm just getting familiarised with the Local Broadcast Messages. I have 2 activities.

MainActivity :

I have 2 buttons. On the click of 1 button, I'm broadcasting the message. On the click of another one, I'm navigating to second Activity.

public class MainActivity extends AppCompatActivity {

Button btn;
    Button btn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.sendBroadCast);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    sendMessage();
            }
        });
        btn1 = (Button)findViewById(R.id.btn);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });

    }
    void sendMessage(){
        Log.d("RAK","Gonna send braodcast");
        Intent intent = new Intent("customMsg");
        intent.putExtra("message", "This is my message!");
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}

Second Activity :

Registering for the receiver in onCreate of this activity.

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.d("RAK","In oncreate of second activity.Registered for local receiver");
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                new IntentFilter("customMsg"));

    }
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Get extra data included in the Intent
            String message = intent.getStringExtra("message");
            Log.d("receiver", "Got message: " + message);
        }
    };

    @Override
    protected void onDestroy() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }
}

The issue I'm facing is, the second Activity is not receiving the broadcast. Please help me.

P.S : Please dont mark this a duplicate. I have followed this link : how to use LocalBroadcastManager?

Thanks, Rakesh

Community
  • 1
  • 1
Rakesh
  • 1
  • 1
  • 3

2 Answers2

2

So as to receive the broadcast the second activity should be up and running while the first one is sending a broadcast, which is going to be hard in your case (2 activities not running at same time).
Your first Activity sends the broadcast, but no activity (in your case second activity) is launched yet so the messgae get 'lost'.
You could test by broadcasting from within a service for example, and your second activity running. Then, the activity could handle/receive it.

What you may want to do is passing a String to the secondActivity using extraData. If you wish to test BroadcastReceiver, then, try with a service sending the broadcast !

HelloSadness
  • 945
  • 7
  • 17
  • It is almost right, but if you will register receiver in oncreate and unregister in ondestroy, than you can send broadcast to background activity. But before it oncreate method should be called. – Pein Nov 13 '16 at 16:02
  • @Anto, Thanks for your reply. However I need more clarification. As you said, 2 activities can't be running simultaneously. Say I have a situation like an Activity is sending a local broadcast (Say it sends a number). I have multiple receivers. The receivers should receive the local broadcast sent by Activity and do accordingly (whatever). How do you achieve this with the Local Broadcast? 2. If I had launched SecondActivity (in the above example) before sending broadcast from initial Activity, would SecondActivity have received it? – Rakesh Nov 24 '16 at 15:21
0

The problem is your registering your broadcast receiver inside onCreate() of second activity, that means the second activity should have been previous launched before you broadcast your intent keeping in mind that your do not unregister it when the second activity is destroyed.

Alternative you can register your receiver statically in the Manifest file

public class Receiver extends BroadcastReceiver{
  public void onReceive(Context context, Intent intent) {
    // Whatever
  }
}

Manifest

<receiver
  android:name=".Receiver"
  android:exported="false" >
  <intent-filter>
    <action android:name="customMsg" />             
  </intent-filter>
</receiver>

NOTE:

Registering statically ensure that the the receiver is registered at system boot time or when the application is added at run time

Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53