The basic use of Android broadcast

here I simply write a dynamic registration broadcast use case

  • sender (requester)

送广播

Intent intent = new Intent();
/ / Set the name of the broadcast (set Action)
intent.setAction("voice_home");
sendBroadcast(intent);
  • 接收者

注册广播

IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("voice_home");
//Register the broadcast
registerReceiver(voiceReceiver, myIntentFilter);

广播接收

private BroadcastReceiver voiceReceiver = new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
         String action = intent.getAction();
         if (action.equals("voice_home")) {
             Intent intent1 = new                Intent(HostMainActivity.this,HostVoiceActivity.class);
             intent1.putExtra("voice_type","home");
             startActivity(intent1);
            }
        }
    };