如何使用LocalBroadcastManager? [英] how to use LocalBroadcastManager?
问题描述
任何一个可以告诉我怎么用/查找LocalBroadcastManager在<一个描述href="http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html">google文档和服务广播DOC ?
can any one tell me how to use/locate LocalBroadcastManager as described in google docs and Service broadcast doc?
我试图谷歌,但世界上没有code可以开始吗?
i tried to google it, but theres 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 dont know where to look for this
任何帮助/对此有何评论?
any help/comment?
更新:我知道如何使用广播,但不知道如何让LocalBroadcastManager可以在我的项目
Update: I know how to use Broadcasts but dont know how to get LocalBroadcastManager available in my project.
推荐答案
我反正回答这个问题。万一有人需要它。
I'll answer this anyway. Just in case someone needs it.
这手表名为事件通知的活动自定义事件名称
。
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);
}
随着code以上,每一个按钮 R.id.button_send
被点击时,一个目的是广播和接收时间mMessageReceiver
在 ReceiverActivity
。
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
.
调试输出应该是这样的:
The debug output should look like this:
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屋!