如何使用LocalBroadcastManager?

如何使用/定位LocalBroadcastManager在谷歌文档和服务广播文档中所述 ?

我试图谷歌,但没有可用的代码开始?

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

任何帮助/评论?

更新 :我知道如何使用广播,但不知道如何让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); } 

使用上面的代码,每次单击buttonR.id.button_send ,Intent都会被广播, mMessageReceiver ReceiverActivitymMessageReceiver ReceiverActivity

debugging输出应该如下所示:

 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! 

我宁愿全面回答。

  1. LocalbroadcastManager包含在android 3.0及以上版本,所以你必须使用支持库v4的早期版本。 看到这里的说明

  2. 创build一个广播接收器:

     private BroadcastReceiver onNotice= new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // intent can contain anydata Log.d("sohail","onReceive called"); tv.setText("Broadcast received !"); } }; 
  3. 将您的接收器注册到以下活动的onResume中:

     protected void onResume() { super.onResume(); IntentFilter iff= new IntentFilter(MyIntentService.ACTION); LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, iff); } //MyIntentService.ACTION is just a public static string defined in MyIntentService. 
  4. 在onPause中unRegister接收器:

     protected void onPause() { super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice); } 
  5. 现在,无论何时从应用程序的活动或服务发送本地广播,onNotice的onReceive将被称为:)。

编辑:你可以在这里阅读完整的教程LocalBroadcastManager:Intra应用程序消息传递

在Eclipse中,最终我不得不添加兼容性/支持库,方法是右键单击我的项目并select:

Android工具 – > 添加支持库

一旦它被添加,然后我能够在我的代码中使用LocalBroadcastManager类。


Android兼容性库

在接收端:

  • 首先注册LocalBroadcast接收器
  • 然后在onReceive中处理传入的意图数据。

      @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver, new IntentFilter("Your_IntentFilter_string")); } public BroadcastReceiver mMessageReceiver = 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("Your_IntentFilter_string"); intent.putExtra("key", "My Data"); //Put your all data using put extra LocalBroadcastManager.getInstance(context).sendBroadcast(intent); 

当你用LocalBroadcastReceiver播放足够的时候,我会build议你给绿色机器人的EventBus一个尝试 – 相比LBR,你一定会意识到它的不同和有用性。 更less的代码,可定制的接收器线程(UI / Bg),检查接收器的可用性,粘性事件,事件可以用作数据传输等。

在开发人员文档中可以find实现LocalBroadcastManager的Activity和Service的示例。 我个人发现它非常有用。

编辑:从那时起,该链接已从网站中删除,但数据如下: https : //github.com/carrot-garden/android_maven-android-plugin-samples/blob/master/support4demos/src/com/例如/机器人/ supportv4 /内容/ LocalServiceBroadcaster.java

我们也可以使用与broadcastManger相同的接口,在这里我是共享广播pipe理器的testing代码,但通过接口。

首先制作一个界面,如:

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

enter code here }

3 – 这是第二个类实现相同的接口,其方法自动调用

  public class Second implements MyInterface{ public void GetName(String name) { System.out.println("Second"+name); } } 

所以通过这种方法我们可以使用与broadcastManager相同的接口。