如何使用/定位在谷歌文档和服务广播文档中描述的LocalBroadcastManager ?
我试着谷歌它,但没有可用的代码开始?
文档说,我应该使用它,如果我想做广播内部与我的应用程序的进程,但我不知道在哪里寻找这个。
任何帮助/评论?
更新:我知道如何使用广播,但不知道如何在我的项目中获得LocalBroadcastManager。
如何使用/定位在谷歌文档和服务广播文档中描述的LocalBroadcastManager ?
我试着谷歌它,但没有可用的代码开始?
文档说,我应该使用它,如果我想做广播内部与我的应用程序的进程,但我不知道在哪里寻找这个。
任何帮助/评论?
更新:我知道如何使用广播,但不知道如何在我的项目中获得LocalBroadcastManager。
当前回答
如何将全局广播更改为局部广播
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);
其他回答
我是一名iOS开发者,所以我做了一个类似于NotificationCenter的解决方案:
object NotificationCenter {
var observers: MutableMap<String, MutableList<NotificationObserver>> = mutableMapOf()
fun addObserver(observer: NotificationObserver, notificationName: NotificationName) {
var os = observers[notificationName.value]
if (os == null) {
os = mutableListOf<NotificationObserver>()
observers[notificationName.value] = os
}
os.add(observer)
}
fun removeObserver(observer: NotificationObserver, notificationName: NotificationName) {
val os = observers[notificationName.value]
if (os != null) {
os.remove(observer)
}
}
fun removeObserver(observer:NotificationObserver) {
observers.forEach { name, mutableList ->
if (mutableList.contains(observer)) {
mutableList.remove(observer)
}
}
}
fun postNotification(notificationName: NotificationName, obj: Any?) {
val os = observers[notificationName.value]
if (os != null) {
os.forEach {observer ->
observer.onNotification(notificationName,obj)
}
}
}
}
interface NotificationObserver {
fun onNotification(name: NotificationName,obj:Any?)
}
enum class NotificationName(val value: String) {
onPlayerStatReceived("on player stat received"),
...
}
一些想要观察通知的类必须符合观察者协议:
class MainActivity : AppCompatActivity(), NotificationObserver {
override fun onCreate(savedInstanceState: Bundle?) {
...
NotificationCenter.addObserver(this,NotificationName.onPlayerStatReceived)
}
override fun onDestroy() {
...
super.onDestroy()
NotificationCenter.removeObserver(this)
}
...
override fun onNotification(name: NotificationName, obj: Any?) {
when (name) {
NotificationName.onPlayerStatReceived -> {
Log.d(tag, "onPlayerStatReceived")
}
else -> Log.e(tag, "Notification not handled")
}
}
最后,向观察者发布一些通知:
NotificationCenter.postNotification(NotificationName.onPlayerStatReceived,null)
使用LocalBroadcastManager的Kotlin版本:
请检查下面的注册代码, 发送和接收广播消息。
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()
}
}
如何将全局广播更改为局部广播
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);
我们也可以使用接口相同的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功能相同的接口。
通过在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.