BroadcastReceiver的不从IntentService接收广播 [英] BroadcastReceiver does not receive Broadcast from IntentService

查看:183
本文介绍了BroadcastReceiver的不从IntentService接收广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从IntentService来启动它的活动发送一个广播,这是我的注册活动中的接收器:

I'm trying to send a broadcast from an IntentService to the activity that started it, this is how i register the Receiver in the activity:

private BroadcastReceiver mInitializer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ....
    mInitializer = new InitializationReceiver();
    IntentFilter initializer = new IntentFilter();
    initializer.addAction(IntentConstants.Tasks.INITIALIZE);
    initializer.addAction(IntentConstants.Initialization.INITIALIZE_IS_FIRST_START);
    initializer.addAction("test");
    registerReceiver(mInitializer, initializer);
    ....
}

private class InitializationReceiver extends BroadcastReceiver {
    private InitializationReceiver() {
        if(D) Log.d(TAG, "Instantiated InitializationReceiver");
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        if(D) Log.d(TAG, "Received broadcast, intentAction: "+intent.getAction());
        if(intent.getAction().equals(IntentConstants.Tasks.INITIALIZE)) {
            if(D) Log.d(TAG, "Received Initialize Intent");
        }
        if(intent.getAction().equals(IntentConstants.Initialization.INITIALIZE_IS_FIRST_START)) {
            if(D) Log.d(TAG, "Received First Start Intent");
        }
    }
}

这是我如何发送广播从IntentService:

And this is how i send the broadcast from the IntentService:

if(D) Log.d(TAG, "Got here");
Intent testIntent = new Intent("test");
sendBroadcast(testIntent);

  • 在该服务中创建的事业来到这里得到记录,
  • 中的BroadcastReceiver正在创建服务之前和初始化
  • 活动是可见的,当广播发送。
  • 广播没有收到,导致接收到的广播......从未登录
    • The service is created cause "Got here" gets logged,
    • the BroadcastReceiver is initialized before the service being created and
    • the activity is visible when the broadcast is sent.
    • the broadcast is not received, cause "Received broadcast..." is never logged
    • 这是什么原因这个问题?

      What could cause this problem?

      推荐答案

      您应该注册注销你的接收器在 onResume()的onPause()分别。因为如果你是在的onCreate只登记()和注销的的onPause(),那么下一次的活动被推上前台,的onCreate()将不会被再次调用,然后它会不会再次注册接收器。但 onResume()总是叫上正在显示的活动。

      You should register and unregister your receivers in onResume() and in onPause() respectively. Cause if you only register it in onCreate() and unregister in onPause(), then the next time the activity is brought to the foreground, onCreate() will not be called again and then it will not register the receiver again. But onResume() is always called on the activity being displayed.

      public void onResume() {
          super.onResume();
          ....
          registerReceiver(myBroadcastReceiver, intentFilter);
      }
      
      public void onPause() {
          super.onPause();
          ...
          unregisterReceiver(myBroadcastReceiver);
      }
      

      这篇关于BroadcastReceiver的不从IntentService接收广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆