LocalBroadcastManager

来源:互联网 发布:Python编程pdf 编辑:程序博客网 时间:2024/05/16 07:06

转自:http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager

类概述


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

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

总结


Public Methodsstatic LocalBroadcastManagergetInstance(Context context)voidregisterReceiver(BroadcastReceiver receiver, IntentFilter filter)
注册对应给定IntentFilter的接收者
booleansendBroadcast(Intent intent)
发送给定intent给有兴趣接收者
voidsendBroadcastSync(Intent intent)
类似sendBroadcast(Intent), but if there are any receivers for the Intent this function will block and immediately dispatch them before returning.
voidunregisterReceiver(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
receiverThe BroadcastReceiver to handle the broadcast.filterSelects the Intent broadcasts to be received.
  See Also
  •     unregisterReceiver(BroadcastReceiver)

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
intentThe Intent to broadcast; all receivers matching this Intent will receive the broadcast.
See Also
  • registerReceiver(BroadcastReceiver, IntentFilter)

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
receiverThe BroadcastReceiver to unregister.
See Also
  • registerReceiver(BroadcastReceiver, IntentFilter)

使用方法

SenderActivity.java

@Overridepublic 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

@Overridepublic 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);  }};@Overrideprotected void onDestroy() {  // Unregister since the activity is about to be closed.  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);  super.onDestroy();}



    0 0