Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Local Broadcasts in Android

Tech Apr 24 11

Local Broadcast Implementation

Local broadcasts using LocalBroadcastManager are confined to the application context.

Sending a Broadcast

Intent intent = new Intent("com.example.update");
LocalBroadcastManager.getInstance(BroadActivity2.this).sendBroadcast(intent);

Registering a Receiver

mBroadcast = new MyBroadcast();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.update");
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcast, intentFilter);

Handling Broadcasts

private class MyBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.example.update")) {
            mTvTest.setText("123");
        }
    }
}

Cleanup

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

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.