LocalBroadcastManager

本文介绍了一种在Android应用内部进行高效、安全广播通信的方法——LocalBroadcastManager。它提供了发送和接收局部广播的功能,相较于全局广播,能更好地保护应用数据隐私,并提高通信效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类概述

提供在本进程中注册并发送广播到本地数据的帮助。与使用sendBroadcast(Intent)发送全局广播相比有很多优势:

  • 广播的数据不会离开本应用,不需要担心私有数据泄露问题
  • 其他应用不可能广播数据到本应用,不需要担心有安全漏洞
  • 比发送全系统的全局广播更有效

总结


Public Methods
static  LocalBroadcastManager getInstance( Context context)
void registerReceiver( BroadcastReceiver receiver,  IntentFilter filter)
注册对应给定IntentFilter的接收者
boolean sendBroadcast( Intent intent)
发送给定intent给有兴趣接收者
void sendBroadcastSync( Intent intent)
类似sendBroadcast(Intent), but if there are any receivers for the Intent this function will block and immediately dispatch them before returning.
void unregisterReceiver( BroadcastReceiver receiver)
注销之前注册的接收者
[Expand]
Inherited Methods
 From class java.lang.Object

公共方法

public static LocalBroadcastManager getInstance (Context context)
public void registerReceiver (BroadcastReceiver receiver, IntentFilter filter)

Register a receive for any local broadcasts that match the given IntentFilter.

Parameters
receiver The BroadcastReceiver to handle the broadcast.
filter Selects the Intent broadcasts to be received.
  See Also
public boolean sendBroadcast (Intent intent)

Broadcast the given intent to all interested BroadcastReceivers. This call is asynchronous; it returns immediately, and you will continue executing while the receivers are run.

Parameters
intent The Intent to broadcast; all receivers matching this Intent will receive the broadcast.
public void sendBroadcastSync (Intent intent)
Like  sendBroadcast(Intent) , but if there are any receivers for the Intent this function will block and immediately dispatch them before returning.
public void unregisterReceiver (BroadcastReceiver receiver)

Unregister a previously registered BroadcastReceiver. All filters that have been registered for this BroadcastReceiver will be removed.

Parameters
receiver The BroadcastReceiver to unregister.

使用方法

SenderActivity.java

  1. @Override  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.   
  4.   ...  
  5.   
  6.   // Every time a button is clicked, we want to broadcast a notification.  
  7.   findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {  
  8.     @Override  
  9.     public void onClick(View v) {  
  10.       sendMessage();  
  11.     }  
  12.   });  
  13. }  
  14.   
  15. // Send an Intent with an action named "custom-event-name". The Intent sent should   
  16. // be received by the ReceiverActivity.  
  17. private void sendMessage() {  
  18.   Log.d("sender""Broadcasting message");  
  19.   Intent intent = new Intent("custom-event-name");  
  20.   // You can also include some extra data.  
  21.   intent.putExtra("message""This is my message!");  
  22.   LocalBroadcastManager.getInstance(this).sendBroadcast(intent);  
  23. }  
@Override
public void onCreate(Bundle savedInstanceState) {

  ...

  // Every time a button is clicked, we want to broadcast a notification.
  findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      sendMessage();
    }
  });
}

// Send an Intent with an action named "custom-event-name". The Intent sent should 
// be received by the ReceiverActivity.
private void sendMessage() {
  Log.d("sender", "Broadcasting message");
  Intent intent = new Intent("custom-event-name");
  // You can also include some extra data.
  intent.putExtra("message", "This is my message!");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

ReceiverActivity.java

  1. @Override  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.   
  4.   ...  
  5.   
  6.   // Register to receive messages.  
  7.   // We are registering an observer (mMessageReceiver) to receive Intents  
  8.   // with actions named "custom-event-name".  
  9.   LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,  
  10.       new IntentFilter("custom-event-name"));  
  11. }  
  12.   
  13. // Our handler for received Intents. This will be called whenever an Intent  
  14. // with an action named "custom-event-name" is broadcasted.  
  15. private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {  
  16.   @Override  
  17.   public void onReceive(Context context, Intent intent) {  
  18.     // Get extra data included in the Intent  
  19.     String message = intent.getStringExtra("message");  
  20.     Log.d("receiver""Got message: " + message);  
  21.   }  
  22. };  
  23.   
  24. @Override  
  25. protected void onDestroy() {  
  26.   // Unregister since the activity is about to be closed.  
  27.   LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);  
  28.   super.onDestroy();  
  29. }  
@Override
public void onCreate(Bundle savedInstanceState) {

  ...

  // Register to receive messages.
  // We are registering an observer (mMessageReceiver) to receive Intents
  // with actions named "custom-event-name".
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
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() {
  // Unregister since the activity is about to be closed.
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值