waq*_*lam 440 android broadcastreceiver
LocalBroadcastManager如谷歌文档和服务广播文档中所述如何使用/定位?
我试图谷歌它,但没有可用的代码开始?
文件说如果我想在我的应用程序进程内部进行广播,我应该使用它,但我不知道在哪里寻找这个.
任何帮助/评论?
更新:我知道如何使用广播,但不知道如何LocalBroadcastManager在我的项目中使用.
Shi*_*iki 835
无论如何我会回答这个问题.以防有人需要它.
监视名为的事件的通知的活动"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();
}
Run Code Online (Sandbox Code Playgroud)
发送/广播通知的第二个活动.
@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);
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,每次R.id.button_send单击按钮时,都会广播一个Intent并由mMessageReceiverin 接收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!
Run Code Online (Sandbox Code Playgroud)
Soh*_*ziz 127
我宁愿全面回答.
LocalbroadcastManager包含在android 3.0及更高版本中,所以你必须使用支持库v4进行早期版本.看这里的说明
创建广播接收器:
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 !");
}
};
Run Code Online (Sandbox Code Playgroud)在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.
Run Code Online (Sandbox Code Playgroud)onPause中的unRegister接收器:
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice);
}
Run Code Online (Sandbox Code Playgroud)现在,每当从应用程序的活动或服务发送localbroadcast时,onReceive on onNotice将被调用:).
编辑:您可以在这里阅读完整的教程LocalBroadcastManager:Intra应用程序消息传递
waq*_*ali 39
在接收结束时:
然后处理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
}
}
};
Run Code Online (Sandbox Code Playgroud)发送结束时:
Intent intent = new Intent("filter_string");
intent.putExtra("key", "My Data");
// put your all data using put extra
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)
waq*_*lam 27
在Eclipse中,最终我必须通过右键单击我的项目并选择以下内容来添加兼容性/支持库:
Android Tools - > Add Support Library
一旦添加,我就可以LocalBroadcastManager在我的代码中使用类.

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
Run Code Online (Sandbox Code Playgroud)
更换
registerReceiver(new YourReceiver(),new IntentFilter("YourAction"));
Run Code Online (Sandbox Code Playgroud)
同
localBroadcastManager.registerReceiver(new YourReceiver(),new IntentFilter("YourAction"));
Run Code Online (Sandbox Code Playgroud)
更换
sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)
同
localBroadcastManager.sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)
更换
unregisterReceiver(mybroadcast);
Run Code Online (Sandbox Code Playgroud)
同
localBroadcastManager.unregisterReceiver(mybroadcast);
Run Code Online (Sandbox Code Playgroud)
androidx.localbroadcastmanager 在 1.1.0 版中已弃用
原因
LocalBroadcastManager是应用程序范围内的事件总线,在您的应用程序中包含违反层的行为;任何组件都可以侦听来自任何其他组件的事件。它继承了系统BroadcastManager的不必要用例限制;开发人员必须使用Intent,即使对象只存在于一个进程中,也永远不要离开它。出于同样的原因,它不遵循基于功能的BroadcastManager。
这些加在一起使开发人员感到困惑。
替代
您可以将的用法替换LocalBroadcastManager为可观察模式的其他实现。根据您的用例,合适的选项可能是LiveData反应流。
LiveData的优势
您可以LiveData使用单例模式扩展对象以包装系统服务,以便可以在您的应用程序中共享它们。该LiveData对象一次连接到系统服务,然后任何需要资源的观察者都可以监视该LiveData对象。
public class MyFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LiveData<BigDecimal> myPriceListener = ...;
myPriceListener.observe(this, price -> {
// Update the UI.
});
}
}
Run Code Online (Sandbox Code Playgroud)
该observe()方法将作为实例的片段LifecycleOwner作为第一个参数。这样做表示此观察者已绑定到Lifecycle与所有者关联的对象,这意味着:
如果Lifecycle对象未处于活动状态,则即使值更改也不会调用观察者。
生命周期对象销毁后,观察者将自动删除
LiveData对象具有生命周期感知这一事实意味着您可以在多个活动,片段和服务之间共享它们。
当您使用 LocalBroadcastReceiver 玩够了时,我建议您尝试一下Green Robot 的 EventBus - 与 LBR 相比,您一定会意识到它的不同之处和实用性。更少的代码,可定制接收者的线程(UI/Bg),检查接收者的可用性,粘性事件,事件可以用作数据传递等。
LocalBroadcastManager:请检查以下代码registering和
sending消息。receivingbroadcast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// register broadcast manager
val localBroadcastManager = LocalBroadcastManager.getInstance(this)
localBroadcastManager.registerReceiver(receiver, IntentFilter("your_action"))
}
// broadcast receiver
var receiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent != null) {
val str = intent.getStringExtra("key")
}
}
}
/**
* Send broadcast method
*/
fun sendBroadcast() {
val intent = Intent("your_action")
intent.putExtra("key", "Your data")
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
override fun onDestroy() {
// Unregister broadcast
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver)
super.onDestroy()
}
}
Run Code Online (Sandbox Code Playgroud)