【问题标题】:My BroadcastReceiver never gets called?我的 BroadcastReceiver 永远不会被调用?
【发布时间】:2014-01-21 14:34:53
【问题描述】:

这个 BroadcastReceiver 在我的片段中:

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ...
    getActivity().startService(new Intent(getActivity(),LightSensor.class));
    Log.d(TAG, "Called the LightSensor Service Class to start");

    IntentFilter luxfilter = new IntentFilter("LuxUpdate");
    getActivity().getApplicationContext().registerReceiver(mLightReceiver, luxfilter);


    ...
    }

...

    // RECEIVE DATA FROM LIGHT SENSOR BROADCAST
        public BroadcastReceiver mLightReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                String lux = intent.getStringExtra("Lux");
                Log.d(TAG, "Recieve Broadcast-Lux Update: " + lux);
                //TextView tvSensorLightLux = (TextView) findViewById(R.id.tvSensorLightLux);
                mLightValue.setText(lux);

            }
        };

问题是我确实认为它是在倾听或接收。 Log.d 从不显示在 LogCat 中。我不知道为什么。这个条目和另一个做的工作的唯一区别是前一个条目实际上是在一个活动中。这是一个片段。我是否在这里遗漏了某些内容,或者我的清单中是否应该有针对此片段或接收器的内容?

更新:

传感器发送广播:

private void sendLuxUpdate() {

        if(isLightSensing){
        Log.d(TAG, "sender Broadcasting message " + Lux);
        Intent intent = new Intent("LuxUpdate");
        intent.putExtra("Lux", Lux);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        }
    }

【问题讨论】:

  • 你注册你的接收器了吗?
  • 对不起,我编辑了上面的内容以反映 reg。在注册中,我确实看到了 Log.d 条目。并不意味着它被正确注册,只是它通过了那个语句。
  • 您还应该包括将Intent 发送给接收者,所以如果一切正常,有人可能会看看。
  • 接收者是否注册与代码无关。
  • 我仍然缺少注册语句,它应该类似于LocalBroadcastManager.getInstance(this).registerReceiver(broadcast_receiver);,因为您使用的是LocalBroadcastManager

标签: android android-fragments broadcastreceiver broadcast


【解决方案1】:

你至少做错了两件事:

  1. 通过Context.registerReceiver() 注册接收器,然后通过LocalBroadcastmanager 发送广播更新。你永远不会收到这些消息。 LocalBroadcastManager 不知道通过 Context 注册的接收者,反之亦然。
  2. 我认为直接在应用程序上下文中注册接收器不是一个好主意,因为这会泄漏片段。

所以试试这个:在onCreateonResume 中注册接收器,然后在互补方法中取消注册相同的实例:onDestroyonPause。此外,在注册和发送 Intent 时,使用相同的机制 - 基于LocalBroadcastManager,或者基于Context。第一个的优点是仅在您的应用中发送消息。

例如,假设您的片段名为DeviceView

public class DeviceView extends Fragment{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(your_receiver, intent_filter);
        /// other code
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(your_receiver);
      /// other code
    }
}

【讨论】:

  • 很有道理。听起来你在说我用一种语言发送并收听另一种语言。我只需要在应用程序中接收这些广播。 onPause 取消注册广播。由于这是一个 Fragment,我应该如何注册 Receivr 是我想坚持使用 LocalBroadcastManager?
  • LocalBroadcastManager.getInstance(getActivity()) ...只是不要忘记在补充方法中注册/取消注册。 ...并且还赞成;)
  • 我试过了,但是 getInstance 抛出一个错误,说它与 Fragment 不兼容
  • 你能说明你是用什么方法尝试的吗?另外,您的片段的基类是什么?
  • public class DeviceView extends Fragment {
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 2013-10-12
  • 2012-03-27
  • 2013-08-29
  • 2013-11-03
相关资源
最近更新 更多