类概述
提供在本进程中注册并发送广播到本地数据的帮助。与使用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
| |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
公共方法
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)
LikesendBroadcast(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.
使用方法
- @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);
- }
@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);
}
- @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();
- }
@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();
}