SoFunction
Updated on 2025-04-10

Detailed explanation of how to use local broadcast in Android learning

Local broadcast information can only be passed inside the application, and the broadcast receiver can only receive broadcast messages inside the application.

MainActivity Code

package ;

import ;
import ;
import ;
import ;
import .;
import .;
import ;
import ;
import ;
import ;

public class MainActivity extends AppCompatActivity implements {

  private Button button;
  private IntentFilter intentFilter;
  private LocalBroadcastManager localBroadcastManager ;
  private LocalReceiver localReciiver;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    button = (Button)findViewById(.send_button);
    (this);
    localBroadcastManager = (this);//use    intentFilter = new IntentFilter();
    (".LOCAL_BROADCAST");
    localReciiver = new LocalReceiver();
    (localReciiver,intentFilter);
  }

  @Override
  protected void onDestroy() {
    ();
    (localReciiver);
  }

  @Override
  public void onClick(View view) {
    Intent intent = new Intent(".LOCAL_BROADCAST");
    (intent);
  }
  class LocalReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
      (context,"received local broadcast",Toast.LENGTH_SHORT).show();
    }
  }
}

First, the instance is obtained through the getInstance(this) method of LocalBroadcastManager (local broadcast management class). When registering a broadcast message, registerReceiver (parameter 1, parameter 2) method of localBroadcastManager instance is registered (parameter 1 is the local broadcast recipient, parameter 2 is that the filter only selects to receive specific broadcast messages), and the broadcast message is called the sendBroadcast (Initent initent) method of localBroadcastManager instance is sent.

MyRecevity

package ;

import ;
import ;
import ;
import ;

public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    (context,"Received in MyBroadCastReceiver",Toast.LENGTH_SHORT).show();
    abortBroadcast();
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
< xmlns:andro
  xmlns:app="/apk/res-auto"
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="">
  <Button
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Send a broadcast"/>

</>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
  package="">

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="" />

        <category android:name="" />
      </intent-filter>
    </activity>

    <receiver
      android:name=".MyReceiver"
      android:enabled="true"
      android:exported="true">
      <intent-filter
        android:priority="100">
        <action android:name=".LOCAL_BROADCAST"/>
      </intent-filter>
    </receiver>
  </application>

</manifest>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.