如何使用/定位在谷歌文档和服务广播文档中描述的LocalBroadcastManager ?
我试着谷歌它,但没有可用的代码开始?
文档说,我应该使用它,如果我想做广播内部与我的应用程序的进程,但我不知道在哪里寻找这个。
任何帮助/评论?
更新:我知道如何使用广播,但不知道如何在我的项目中获得LocalBroadcastManager。
如何使用/定位在谷歌文档和服务广播文档中描述的LocalBroadcastManager ?
我试着谷歌它,但没有可用的代码开始?
文档说,我应该使用它,如果我想做广播内部与我的应用程序的进程,但我不知道在哪里寻找这个。
任何帮助/评论?
更新:我知道如何使用广播,但不知道如何在我的项目中获得LocalBroadcastManager。
当前回答
接收端:
First register LocalBroadcast Receiver Then handle incoming intent data in onReceive. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); lbm.registerReceiver(receiver, new IntentFilter("filter_string")); } public BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent != null) { String str = intent.getStringExtra("key"); // get all your data from intent and do what you want } } };
发送端:
Intent intent = new Intent("filter_string");
intent.putExtra("key", "My Data");
// put your all data using put extra
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
其他回答
在Eclipse中,最终我不得不通过右键单击我的项目并选择添加兼容性/支持库:
Android工具->添加支持库
一旦添加了它,我就可以在代码中使用LocalBroadcastManager类了。
我还是要回答这个问题。以防有人需要。
ReceiverActivity.java
一个监视名为“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
第二个活动发送/广播通知。
@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接收。
调试输出应该是这样的:
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!
我们也可以使用接口相同的broadcastManger在这里我分享broadcastManager的测试代码,但通过接口。
首先,创建如下界面:
public interface MyInterface {
void GetName(String name);
}
2-这是第一个需要实现的类
public class First implements MyInterface{
MyInterface interfc;
public static void main(String[] args) {
First f=new First();
Second s=new Second();
f.initIterface(s);
f.GetName("Paddy");
}
private void initIterface(MyInterface interfc){
this.interfc=interfc;
}
public void GetName(String name) {
System.out.println("first "+name);
interfc.GetName(name);
}
}
3-这里是实现相同接口的第二个类,其方法自动调用
public class Second implements MyInterface{
public void GetName(String name) {
System.out.println("Second"+name);
}
}
因此,通过这种方法,我们可以使用与broadcastManager功能相同的接口。
接收端:
First register LocalBroadcast Receiver Then handle incoming intent data in onReceive. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); lbm.registerReceiver(receiver, new IntentFilter("filter_string")); } public BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent != null) { String str = intent.getStringExtra("key"); // get all your data from intent and do what you want } } };
发送端:
Intent intent = new Intent("filter_string");
intent.putExtra("key", "My Data");
// put your all data using put extra
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
如何将全局广播更改为局部广播
1)创建实例
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
2)用于注册BroadcastReceiver
取代
registerReceiver(new YourReceiver(),new IntentFilter("YourAction"));
与
localBroadcastManager.registerReceiver(new YourReceiver(),new IntentFilter("YourAction"));
3)用于发送广播信息
取代
sendBroadcast(intent);
与
localBroadcastManager.sendBroadcast(intent);
4)注销广播信息
取代
unregisterReceiver(mybroadcast);
与
localBroadcastManager.unregisterReceiver(mybroadcast);