gpt4 book ai didi

java - 未收到广播[LocalBroadcastManager] Android

转载 作者:行者123 更新时间:2023-12-02 05:01:01 25 4
gpt4 key购买 nike

你能帮我解决这个问题吗?我已经实现了一个接收器(通过 XML 注册),它监听特定的本地广播,然后启动一个服务进行进一步处理,但不知何故该接收器没有接收任何广播。

虽然另一个通过代码在本地注册的接收器能够接收广播,但你能帮我解决这个问题吗?下面是我的代码。

// Sending broadcast
Intent intent = new Intent(Constants.ACTION_PROFILE_UPDATED);
LocalBroadcastManager.getInstance(POC.getAppContext()).sendBroadcast(intent);

// Receiver
public class LocalReceiver extends BroadcastReceiver {

private final String TAG = LocalReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "received"); // its not received
if(intent.getAction() != null){
String action = intent.getAction();
Log.i(TAG, "action = " + action);

if(action.equals(Constants.ACTION_PROFILE_UPDATED)){


// IN manifest
<receiver
android:name=".LocalReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="local.action.profile.updated" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

这个该死的代码不起作用,开发者指南中没有任何地方说通过 xml 注册的接收器不会接收本地广播。

请帮忙,谢谢。

最佳答案

I have implemented a receiver(registered via XML) which listens for particular local broadcasts

这是不可能的。 LocalBroadcastManager 不适用于 list 注册的接收器,只能用于通过 registerReceiver() 注册的接收器,并在 LocalBroadcastManager 实例本身上调用。

关于java - 未收到广播[LocalBroadcastManager] Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28284623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com