如何使用 LocalBroadcastManager? [英] How to use LocalBroadcastManager?

查看:20
本文介绍了如何使用 LocalBroadcastManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用/定位 LocalBroadcastManager,如 google 文档服务广播文档?

How to use/locate LocalBroadcastManager as described in google docs and Service broadcast doc?

我试着用谷歌搜索,但没有可用的代码?

I tried to google it, but there is no code available to start with?

文档说如果我想在我的应用程序的进程中进行内部广播,我应该使用它,但我不知道去哪里找这个.

The documents say that I should use it if I want to do broadcast internally with in my app's process but I don't know where to look for this.

任何帮助/评论?

更新:我知道如何使用广播,但不知道如何在我的项目中使用 LocalBroadcastManager.

Update: I know how to use Broadcasts but don't know how to get LocalBroadcastManager available in my project.

推荐答案

无论如何我都会回答这个问题.以防万一有人需要它.

I'll answer this anyway. Just in case someone needs it.

监视名为 "custom-event-name" 的事件通知的 Activity.

An activity that watches for notifications for the event named "custom-event-name".

@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();
}

SenderActivity.java

发送/广播通知的第二个活动.

SenderActivity.java

The second activity that sends/broadcasts notifications.

@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);
}

通过上面的代码,每次点击按钮R.id.button_send时,都会广播一个Intent并由ReceiverActivity中的mMessageReceiver接收代码>.

With the code above, every time the button R.id.button_send is clicked, an Intent is broadcasted and is received by mMessageReceiver in ReceiverActivity.

调试输出应如下所示:

01-16 10:35:42.413: D/sender(356): Broadcasting message
01-16 10:35:42.421: D/receiver(356): Got message: This is my message! 

这篇关于如何使用 LocalBroadcastManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆