为什么 LocalBroadcastManager 不能代替 Context.registerReceiver 工作?

Why would LocalBroadcastManager not work instead of Context.registerReceiver?

我必须为此应用程序实现一项功能,该功能由一个 Activity 和一个在后台工作的 Service 组成(它实现了 Service,而不是 IntentService) .

网上看了几个教程,应该可以的,都是用的LocalBroadcastManager,顺便说下Android推荐的:

If you don't need to send broadcasts across applications, consider using this class with LocalBroadcastManager instead of the more general facilities described below.

我真的浪费了一天的时间来找出为什么它对我不起作用的问题:它只有在我使用 Context.sendBroadcast() 时才有效。和 Context.registerReceiver() 而不是 LocalBroadcastManager 方法。

现在我的应用程序可以正常运行,但我觉得我违背了最佳做法,而且我不知道为什么。 知道为什么会发生吗?

编辑:

在我写完这个问题之后,我进一步研究了这个问题。 LocalBroadcastManager 通过单例工作,我们应该这样称呼 LocalBroadcastManager.getInstance(this).method()。我记录了两个实例(在 ActivityService 中)并且它们具有不同的内存地址。 现在我想到另一个问题,Service 不应该和调用它的 Activity 有相同的 Context 吗?从 this article 开始,一个服务在主线程上运行,因此我认为 Context 会是 相同。

有什么想法吗? (对不起长post)

代码示例:

我的服务

public class MyService extends Service {

...

// When an event is triggered, sends a broadcast

Intent myIntent = new Intent(MainActivity.MY_INTENT);
myIntent.putExtra("myMsg","msg");
sendBroadcast(myIntent);

// Previously I was trying:
// LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(myIntent);

}

MyActivity

public class MainActivity {

...

private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) { 
            Log.d("onReceive", "received!");
            // TODO something
        }
    };

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(messageReceiver, new IntentFilter(MY_INTENT));
    // Previously I was trying:
    // LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(messageReceiver, new IntentFilter(MY_INTENT));
}
}

声明:

private BroadcastReceiver receiver;

初始化:

receiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //todo
    }
};

报名人数:

LocalBroadcastManager.getInstance(context).registerReceiver(receiver, new IntentFilter("RECEIVER_FILTER"));

context可以是任意类型Context,可以使用application context.

注销:

LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);

广播:

Intent intent = new Intent("RECEIVER_FILTER");
intent.putExtra("EXTRA", someExtra);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

我从未使用过 LocalBroadcastManager,但听起来您必须 register your receiver on there(即 lbm.registerReceiver(...),而不是 mycontext.registerReceiver(...))。你在做吗?

Now I came to another question, shouldn't a Service have the same Context as the Activity that called it? From this article a Service runs on the Main Thread, hence I'd think the Context would be the same.

上下文class 与线程无关。事实上,Service 和 Activity 都是 Context 的(间接)子class——所以它们是它们自己的 Context!
这就是为什么您可以使用 "this" 作为上下文。

但是无论您将哪个上下文发送到 LocalBroadcastManager.getInstance(),您都应该将 exact same LBM instance 发送出去。我想不出任何你不这样做的理由——除非你是 运行 Activity 和不同进程中的服务?

检查你的Service和Activity是否在不同的进程中,LocalBroadcastManager不能在不同的进程中应用。(你应该在AndroidManifest.xml文件中看到它)