如何使用/定位在谷歌文档和服务广播文档中描述的LocalBroadcastManager ?
我试着谷歌它,但没有可用的代码开始?
文档说,我应该使用它,如果我想做广播内部与我的应用程序的进程,但我不知道在哪里寻找这个。
任何帮助/评论?
更新:我知道如何使用广播,但不知道如何在我的项目中获得LocalBroadcastManager。
如何使用/定位在谷歌文档和服务广播文档中描述的LocalBroadcastManager ?
我试着谷歌它,但没有可用的代码开始?
文档说,我应该使用它,如果我想做广播内部与我的应用程序的进程,但我不知道在哪里寻找这个。
任何帮助/评论?
更新:我知道如何使用广播,但不知道如何在我的项目中获得LocalBroadcastManager。
当前回答
我更愿意全面地回答。
LocalbroadcastManager included in android 3.0 and above so you have to use support library v4 for early releases. see instructions here Create a broadcast receiver: 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 !"); } }; Register your receiver in onResume of activity like: 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. unRegister receiver in onPause: protected void onPause() { super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice); } Now whenever a localbroadcast is sent from applications' activity or service, onReceive of onNotice will be called :).
编辑:你可以在这里阅读完整的教程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!
在Eclipse中,最终我不得不通过右键单击我的项目并选择添加兼容性/支持库:
Android工具->添加支持库
一旦添加了它,我就可以在代码中使用LocalBroadcastManager类了。
通过在AndroidManifest.xml文件中声明一个标签(也称为static)
<receiver android:name=".YourBrodcastReceiverClass" android:exported="true">
<intent-filter>
<!-- The actions you wish to listen to, below is an example -->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
你会注意到上面声明的广播接收器有一个属性export = " true "。此属性告诉接收器它可以接收来自应用程序范围之外的广播。 2. 或者通过向registerReceiver动态注册一个实例(即所谓的上下文注册)
public abstract Intent registerReceiver (BroadcastReceiver receiver,
IntentFilter filter);
public void onReceive(Context context, Intent intent) {
//Implement your logic here
}
There are three ways to send broadcasts: The sendOrderedBroadcast method, makes sure to send broadcasts to only one receiver at a time. Each broadcast can in turn, pass along data to the one following it, or to stop the propagation of the broadcast to the receivers that follow. The sendBroadcast is similar to the method mentioned above, with one difference. All broadcast receivers receive the message and do not depend on one another. The LocalBroadcastManager.sendBroadcast method only sends broadcasts to receivers defined inside your application and does not exceed the scope of your application.
enter code here if (createSuccses){
val userDataChange=Intent(BRODCAST_USER_DATA_CHANGE)
LocalBroadcastManager.getInstance(this).sendBroadcast(
userDataChange
)
enableSpinner(false)
finish()
我更愿意全面地回答。
LocalbroadcastManager included in android 3.0 and above so you have to use support library v4 for early releases. see instructions here Create a broadcast receiver: 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 !"); } }; Register your receiver in onResume of activity like: 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. unRegister receiver in onPause: protected void onPause() { super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice); } Now whenever a localbroadcast is sent from applications' activity or service, onReceive of onNotice will be called :).
编辑:你可以在这里阅读完整的教程LocalBroadcastManager:内部应用程序消息传递