简体   繁体   中英

My Activity not receiving Local Broadcasts sent from another activity

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.

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

Thanks, Rakesh

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 !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM